Appearance
mem
▸ mem(fn
, «destructured»?
): (...arguments_
: any
[]) => any
Memoize functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input.
参数
Name | Type | Description |
---|---|---|
fn | any | Function to be memoized. |
«destructured» | Object | - |
› cache | undefined | Map <any , any > | - |
› cacheKey | any | - |
› maxAge | any | - |
返回值
fn
▸ (...arguments_
): any
参数
Name | Type |
---|---|
...arguments_ | any [] |
返回值
any
示例
import mem from 'mem';
let index = 0;
const counter = () => ++index;
const memoized = mem(counter);
memoized('foo');
//=> 1
// Cached as it's the same argument
memoized('foo');
//=> 1
// Not cached anymore as the arguments changed
memoized('bar');
//=> 2
memoized('bar');
//=> 2