title: Why is recursion faster when using a global variable rather than a local one for memoization?
body: <p>I noticed something peculiar when attempting to speed up a recursive function. When I made the object I was employing for memoization an argument to be passed to the function rather than a global variable, the code execution became considerably slower.</p>
  
  <p>So instead of this:</p>
  
  <pre><code>var memoizationObj = {};
  function factorial(n) {
    // factorial code
  };
  
  var a = factorial(10);
  </code></pre>
  
  <p>I changed the code to this:</p>
  
  <pre><code>function factorial(n, memoizationObj) {
    // factorial code
  };
  var b = factorial(10, {});
  </code></pre>
  
  <p>And the code execution became > 50x slower.</p>
  
  <p><img src="https://i.stack.imgur.com/cbR8M.png" alt="jsperf"></p>
  
  <p><strong>Why is this happening?</strong> Here is the <a href="http://jsperf.com/recursion-with-global-obj-vs-arg" rel="nofollow noreferrer"><strong>jsperf</strong></a> with the full code.</p>