R
fn

memoize

v0.0.10tested

Caches the result of a function by its arguments

Examples

ts
const slow = memoize((n: number) => expensive(n));
slow(2); // computed
slow(2); // cached
ts
const sum = memoize((a: number, b: number) => a + b, (a, b) => `${a},${b}`);

Signature

ts
export function memoize<T extends AnyFunction>(fn: T, resolver?: MemoizeResolver<T>): MemoizedFunction<T>{ ... }

Type Parameters

Textends AnyFunction

Parameters

ParameterTypeDescription
fnTThe function to memoize
resolver?MemoizeResolver<T> | undefinedMaps the arguments to a cache key; defaults to the first argument

Returns

MemoizedFunction<T>The memoized function, exposing its `cache` and a `clear()` method