fn
memoize
v0.0.10testedCaches the result of a function by its arguments
Examples
ts
const slow = memoize((n: number) => expensive(n));
slow(2); // computed
slow(2); // cachedts
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 AnyFunctionParameters
| Parameter | Type | Description |
|---|---|---|
fn | T | The function to memoize |
resolver? | MemoizeResolver<T> | undefined | Maps the arguments to a cache key; defaults to the first argument |
Returns
MemoizedFunction<T>The memoized function, exposing its `cache` and a `clear()` method