Skip to content

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.

参数

NameTypeDescription
fnanyFunction to be memoized.
«destructured»Object-
› cacheundefined | Map<any, any>-
› cacheKeyany-
› maxAgeany-

返回值

fn

▸ (...arguments_): any

参数
NameType
...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

源码

memoize.js