title: How underscore memoize is implemented in javascript
body: <p>I'm developing my own functional-programming library, and now referring the <code>underscore</code>. </p>
  
  <p><a href="http://underscorejs.org/#memoize" rel="noreferrer"><strong>memoize</strong>   _.memoize(function, [hashFunction])</a> </p>
  
  <blockquote>
    <p>Memoizes a given function by caching the computed result. Useful for speeding up slow-running computations. 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.</p>
  </blockquote>
  
  <pre><code>var fibonacci = _.memoize(function(n) {
    return n &lt; 2 ? n: fibonacci(n - 1) + fibonacci(n - 2);
  });
  </code></pre>
  
  <p>The above code that enables automatic memorisation without dealing <code>array</code> looks sort of magic, and I saw the source-code below, but still the inner design is not clear to me.</p>
  
  <pre><code> // Memoize an expensive function by storing its results.
    _.memoize = function(func, hasher) {
      var memoize = function(key) {
        var cache = memoize.cache;
        var address = hasher ? hasher.apply(this, arguments) : key;
        if (!_.has(cache, address)) cache[address] = func.apply(this, arguments);
        return cache[key];
      };
      memoize.cache = {};
      return memoize;
    };
  </code></pre>
  
  <p>Can someone give me a brief idea of what is going on?</p>
  
  <p>Appreciated.</p>