UNPKG

memize

Version:

Unabashedly-barebones memoization library with an aim toward speed

82 lines (81 loc) 2.02 kB
export = memize; /** * Memize options object. * * @typedef MemizeOptions * * @property {number} [maxSize] Maximum size of the cache. */ /** * Internal cache entry. * * @typedef MemizeCacheNode * * @property {?MemizeCacheNode|undefined} [prev] Previous node. * @property {?MemizeCacheNode|undefined} [next] Next node. * @property {Array<*>} args Function arguments for cache * entry. * @property {*} val Function result. */ /** * Properties of the enhanced function for controlling cache. * * @typedef MemizeMemoizedFunction * * @property {()=>void} clear Clear the cache. */ /** * Accepts a function to be memoized, and returns a new memoized function, with * optional options. * * @template {(...args: any[]) => any} F * * @param {F} fn Function to memoize. * @param {MemizeOptions} [options] Options object. * * @return {((...args: Parameters<F>) => ReturnType<F>) & MemizeMemoizedFunction} Memoized function. */ declare function memize<F extends (...args: any[]) => any>(fn: F, options?: MemizeOptions | undefined): ((...args: Parameters<F>) => ReturnType<F>) & MemizeMemoizedFunction; declare namespace memize { export { MemizeOptions, MemizeCacheNode, MemizeMemoizedFunction }; } /** * Memize options object. */ type MemizeOptions = { /** * Maximum size of the cache. */ maxSize?: number | undefined; }; /** * Properties of the enhanced function for controlling cache. */ type MemizeMemoizedFunction = { /** * Clear the cache. */ clear: () => void; }; /** * Internal cache entry. */ type MemizeCacheNode = { /** * Previous node. */ prev?: (MemizeCacheNode | undefined) | null; /** * Next node. */ next?: (MemizeCacheNode | undefined) | null; /** * Function arguments for cache * entry. */ args: Array<any>; /** * Function result. */ val: any; };