memoize source npm
_.memoize(func, [resolver])
Creates a function that memoizes the result of func
. If resolver
is
provided it determines the cache key for storing the result based on the
arguments provided to the memoized function. By default, the first argument
provided to the memoized function is used as the map cache key. The func
is invoked with the this
binding of the memoized function.
Note: The cache is exposed as the cache
property on the memoized
function. Its creation may be customized by replacing the _.memoize.Cache
constructor with one whose instances implement the Map
method interface of get
, has
, and set
.
Arguments
- func (Function)
The function to have its output memoized.
- [resolver] (Function)
The function to resolve the cache key.
Returns (Function)
Returns the new memoizing function.
Example
var object = { 'a': 1, 'b': 2 };
var other = { 'c': 3, 'd': 4 };
var values = _.memoize(_.values);
values(object);
// => [1, 2]
values(other);
// => [3, 4]
object.a = 2;
values(object);
// => [1, 2]
// modifying the result cache
values.cache.set(object, ['a', 'b']);
values(object);
// => ['a', 'b']
// replacing `_.memoize.Cache`
_.memoize.Cache = WeakMap;