title: How does memoizing a recursive factorial function make it more efficient?
body: <pre><code>var lookup = {};
  function memoized(n) {
    if(n &lt;= 1) { return 1; }
  
    if(lookup[n]) {
      return lookup[n];
    }
  
    lookup[n] = n * memoized(n - 1);
    return lookup[n];
  }
  </code></pre>
  
  <p>vs.</p>
  
  <pre><code>function fact(n) {
    if(n &lt;= 1) { return 1; }
    return n * fact(n-1);
  }
  </code></pre>
  
  <p>If we call fact(3)</p>
  
  <p>With the second method we get --> 3 * (2 * (1))</p>
  
  <p>What is the efficiency gain of storing the result in a hash.  Is it only for subsequent calls to the same function? I can't see how you would gain anything if you are only calling the function once.</p>
  
  <p>With the memoized Fibonacci function, even if there is only one function call there is still an efficiency gain. To get the nth fibonacci number, if you do not memoize, you will be repeating the calculation for fib(n-1) and fib(n-2) on each fib(n).  I don't see this happening in the factorial function.</p>