How underscore memoize is implemented in javascript

三世轮回 提交于 2019-12-03 16:25:27

memoize has a cache (memoize.cache = {}) that uses for storing the result of the function call. When it's called, it determines an address to store the result by two means: either a call to the hasher function, or the key parameter.

The hasher function works like this (from the underscore page):

If passed an optional hashFunction, it will be used to compute the hash key for storing the result, based on the arguments to the original function. The default hashFunction just uses the first argument to the memoized function as the key.

Then, it calls the function you passed func.apply(...), and stores the result at cache[address].

The second time you call the memoized function, the result will already be in cache (!_.has(..) will return false) and the computation won't be repeated.

I dont' understand why it returns cache[key] and not cache[address] tough...seems to me that cache[address] would be the correct choice.

Update

As pointed out in the comments, the code you present is not the latest implementation of memoize. This is the latest implementation (1.6.0):

_.memoize = function(func, hasher) {
    var memo = {};
    hasher || (hasher = _.identity);
    return function() {
      var key = hasher.apply(this, arguments);
      return _.has(memo, key) ? memo[key] : (memo[key] = func.apply(this, arguments));
    };
  }; 

It works the same way, except from the fact that it is a little more elegant; if an hasher function it's not provided, it uses _.identity as a key, that is a function that simply returns the value passed as an argument:

_.identity = function(value) { return value; }

Aside from this, cache is now called memo but works the same way.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!