_.memoize(func, [resolver])
105
_.memoize(func, [resolver])
创建一个可以缓存func的结果的函数,resolver用来解析缓存result的键
将计算结果缓存在闭包中的私有变量中,如果再次计算同样的值就直接获取
创建一个缓存func方法结果的函数。如果传递了resolver,这个函数用来计算存储结果的缓存key值基于提供给缓存方法的参数。默认情况下,提供给缓存函数的第一个参数被作为map缓存的key。
参数
func (Function): 需要缓存结果的函数
[resolver] (Function): 用来计算缓存key的函数
返回值
(Function): 返回新的缓存函数
例子
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] // Modify the result cache. values.cache.set(object, ['a', 'b']); values(object); // => ['a', 'b'] // Replace `_.memoize.Cache`. _.memoize.Cache = WeakMap;
源代码:
import MapCache from './.internal/MapCache.js' /** * 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`](http://ecma-international.org/ecma-262/7.0/#sec-properties-of-the-map-prototype-object) * method interface of `clear`, `delete`, `get`, `has`, and `set`. * * @since 0.1.0 * @category Function * @param {Function} func The function to have its output memoized. * @param {Function} [resolver] The function to resolve the cache key. * @returns {Function} Returns the new memoized function. * @example * * const object = { 'a': 1, 'b': 2 } * const other = { 'c': 3, 'd': 4 } * * const values = memoize(values) * values(object) * // => [1, 2] * * values(other) * // => [3, 4] * * object.a = 2 * values(object) * // => [1, 2] * * // Modify the result cache. * values.cache.set(object, ['a', 'b']) * values(object) * // => ['a', 'b'] * * // Replace `memoize.Cache`. * memoize.Cache = WeakMap */ //创建一个可以缓存func的结果的函数,resolver用来解析缓存result的键 //将计算结果缓存在闭包中的私有变量中,如果再次计算同样的值就直接获取 //创建一个缓存func方法结果的函数。如果传递了resolver,这个函数用来计算存储结果的缓存key值基于提供给缓存方法的参数。默认情况下,提供给缓存函数的第一个参数被作为map缓存的key。 function memoize(func, resolver) { if (typeof func != 'function' || (resolver != null && typeof resolver != 'function')) { //func和resolver(如果提供的话)如果不是函数,抛错误 throw new TypeError('Expected a function') } const memoized = function(...args) { const key = resolver ? resolver.apply(this, args) : args[0] //如果提供了resolver,就用resolver来确定key,否则key就是memoized的第一个参数 const cache = memoized.cache//cashe对象 if (cache.has(key)) {//如果cache中已经有此key,就直接获取后返回 return cache.get(key) } const result = func.apply(this, args)//否则调用func计算出key对应的value memoized.cache = cache.set(key, result) || cache//将新value存入cache后返回 return result } memoized.cache = new (memoize.Cache || MapCache)//cache使用自定义的MapCache类型 return memoized } memoize.Cache = MapCache export default memoize