Type Alias MemoizeOptions<Fn, Key, Cache>Experimental
MemoizeOptions <Fn, Key, Cache>: {
cache?: Cache;
errorIsCacheable?: ((err: unknown) => boolean);
getKey?: ((this: ThisParameterType<Fn>, ...args: Parameters<Fn>) => Key);
}
cache?: Cache;
errorIsCacheable?: ((err: unknown) => boolean);
getKey?: ((this: ThisParameterType<Fn>, ...args: Parameters<Fn>) => Key);
}
Type Parameters
- Fn extends ((...args: never[]) => unknown)
The type of the function to memoize.
- Key
The type of the cache key.
- Cache extends MemoizationCache<Key, MemoizationCacheResult<ReturnType<Fn>>>
The type of the cache.
Type declaration
OptionalExperimentalcache?: CacheProvide a custom cache for getting previous results. By default, a new Map object is instantiated upon memoization and used as a cache, with no limit on the number of results to be cached.
Alternatively, you can supply a
LruCachewith a specified max size to limit memory usage.OptionalerrorIs ?: ((err: unknown) => boolean)Cacheable Callback to determine if an error or other thrown value is cacheable.
- (err): boolean
Parameters
- err: unknown
The thrown error or other value.
Returns boolean
trueif the error is cacheable,falseotherwise.- err: unknown
OptionalgetKey ?: ((this: ThisParameterType<Fn>, ...args: Parameters<Fn>) => Key)Function to get a unique cache key from the function's arguments. By default, a composite key is created from all the arguments plus the
thisvalue, using reference equality to check for equivalence.Example
import { memoize } from "@std/cache";
import { assertEquals } from "@std/assert";
const fn = memoize(({ value }: { cacheKey: number; value: number }) => {
return value;
}, { getKey: ({ cacheKey }) => cacheKey });
assertEquals(fn({ cacheKey: 1, value: 2 }), 2);
assertEquals(fn({ cacheKey: 1, value: 99 }), 2);
assertEquals(fn({ cacheKey: 2, value: 99 }), 99);
Options for
memoize.UNSTABLE: New API, yet to be vetted.