@formatjs/fast-memoize
Advanced tools
+1
-1
@@ -25,3 +25,3 @@ type Func = (...args: any[]) => any; | ||
| } | ||
| export declare function memoize<F extends Func>(fn: F, options?: Options<F>): F | ((arg: any) => any); | ||
| export declare function memoize<F extends Func>(fn: F, options?: Options<F>): F; | ||
| export type StrategyFn = <F extends Func>(this: unknown, fn: F, cache: DefaultCache<string, ReturnType<F>>, serializer: Serializer, arg: any) => any; | ||
@@ -28,0 +28,0 @@ export interface Strategies<F extends Func> { |
+2
-6
@@ -1,9 +0,5 @@ | ||
| "use strict"; | ||
| // | ||
| // Main | ||
| // | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.strategies = void 0; | ||
| exports.memoize = memoize; | ||
| function memoize(fn, options) { | ||
| export function memoize(fn, options) { | ||
| var cache = options && options.cache ? options.cache : cacheDefault; | ||
@@ -81,5 +77,5 @@ var serializer = options && options.serializer ? options.serializer : serializerDefault; | ||
| }; | ||
| exports.strategies = { | ||
| export var strategies = { | ||
| variadic: strategyVariadic, | ||
| monadic: strategyMonadic, | ||
| }; |
+7
-3
| { | ||
| "name": "@formatjs/fast-memoize", | ||
| "description": "fork of fast-memoize and support esm", | ||
| "version": "2.2.7", | ||
| "version": "3.0.0", | ||
| "license": "MIT", | ||
| "author": "Long Ho <holevietlong@gmail.com>", | ||
| "type": "module", | ||
| "sideEffects": false, | ||
| "types": "index.d.ts", | ||
| "exports": { | ||
| ".": "./index.js" | ||
| }, | ||
| "dependencies": { | ||
@@ -18,5 +24,3 @@ "tslib": "^2.8.0" | ||
| ], | ||
| "main": "index.js", | ||
| "module": "lib/index.js", | ||
| "repository": "formatjs/formatjs.git" | ||
| } |
| type Func = (...args: any[]) => any; | ||
| export interface Cache<K, V> { | ||
| create: CacheCreateFunc<K, V>; | ||
| } | ||
| interface CacheCreateFunc<K, V> { | ||
| (): DefaultCache<K, V>; | ||
| } | ||
| interface DefaultCache<K, V> { | ||
| get(key: K): V | undefined; | ||
| set(key: K, value: V | undefined): void; | ||
| } | ||
| export type Serializer = (args: any[]) => string; | ||
| export interface Options<F extends Func> { | ||
| cache?: Cache<string, ReturnType<F>>; | ||
| serializer?: Serializer; | ||
| strategy?: MemoizeFunc<F>; | ||
| } | ||
| export interface ResolvedOptions<F extends Func> { | ||
| cache: Cache<string, ReturnType<F>>; | ||
| serializer: Serializer; | ||
| } | ||
| export interface MemoizeFunc<F extends Func> { | ||
| (fn: F, options?: Options<F>): F; | ||
| } | ||
| export declare function memoize<F extends Func>(fn: F, options?: Options<F>): F | ((arg: any) => any); | ||
| export type StrategyFn = <F extends Func>(this: unknown, fn: F, cache: DefaultCache<string, ReturnType<F>>, serializer: Serializer, arg: any) => any; | ||
| export interface Strategies<F extends Func> { | ||
| variadic: MemoizeFunc<F>; | ||
| monadic: MemoizeFunc<F>; | ||
| } | ||
| export declare const strategies: Strategies<Func>; | ||
| export {}; |
-80
| // | ||
| // Main | ||
| // | ||
| export function memoize(fn, options) { | ||
| var cache = options && options.cache ? options.cache : cacheDefault; | ||
| var serializer = options && options.serializer ? options.serializer : serializerDefault; | ||
| var strategy = options && options.strategy ? options.strategy : strategyDefault; | ||
| return strategy(fn, { | ||
| cache: cache, | ||
| serializer: serializer, | ||
| }); | ||
| } | ||
| // | ||
| // Strategy | ||
| // | ||
| function isPrimitive(value) { | ||
| return (value == null || typeof value === 'number' || typeof value === 'boolean'); // || typeof value === "string" 'unsafe' primitive for our needs | ||
| } | ||
| function monadic(fn, cache, serializer, arg) { | ||
| var cacheKey = isPrimitive(arg) ? arg : serializer(arg); | ||
| var computedValue = cache.get(cacheKey); | ||
| if (typeof computedValue === 'undefined') { | ||
| computedValue = fn.call(this, arg); | ||
| cache.set(cacheKey, computedValue); | ||
| } | ||
| return computedValue; | ||
| } | ||
| function variadic(fn, cache, serializer) { | ||
| var args = Array.prototype.slice.call(arguments, 3); | ||
| var cacheKey = serializer(args); | ||
| var computedValue = cache.get(cacheKey); | ||
| if (typeof computedValue === 'undefined') { | ||
| computedValue = fn.apply(this, args); | ||
| cache.set(cacheKey, computedValue); | ||
| } | ||
| return computedValue; | ||
| } | ||
| function assemble(fn, context, strategy, cache, serialize) { | ||
| return strategy.bind(context, fn, cache, serialize); | ||
| } | ||
| function strategyDefault(fn, options) { | ||
| var strategy = fn.length === 1 ? monadic : variadic; | ||
| return assemble(fn, this, strategy, options.cache.create(), options.serializer); | ||
| } | ||
| function strategyVariadic(fn, options) { | ||
| return assemble(fn, this, variadic, options.cache.create(), options.serializer); | ||
| } | ||
| function strategyMonadic(fn, options) { | ||
| return assemble(fn, this, monadic, options.cache.create(), options.serializer); | ||
| } | ||
| // | ||
| // Serializer | ||
| // | ||
| var serializerDefault = function () { | ||
| return JSON.stringify(arguments); | ||
| }; | ||
| // | ||
| // Cache | ||
| // | ||
| var ObjectWithoutPrototypeCache = /** @class */ (function () { | ||
| function ObjectWithoutPrototypeCache() { | ||
| this.cache = Object.create(null); | ||
| } | ||
| ObjectWithoutPrototypeCache.prototype.get = function (key) { | ||
| return this.cache[key]; | ||
| }; | ||
| ObjectWithoutPrototypeCache.prototype.set = function (key, value) { | ||
| this.cache[key] = value; | ||
| }; | ||
| return ObjectWithoutPrototypeCache; | ||
| }()); | ||
| var cacheDefault = { | ||
| create: function create() { | ||
| return new ObjectWithoutPrototypeCache(); | ||
| }, | ||
| }; | ||
| export var strategies = { | ||
| variadic: strategyVariadic, | ||
| monadic: strategyMonadic, | ||
| }; |
Yes
NaN5363
-41.2%5
-28.57%112
-50.88%