title: Performance of function declaration vs function expressions in javascript in a loop
body: <p>In the following jsperf: <a href="http://jsperf.com/defined-function-vs-in-loop-function/3" rel="nofollow">http://jsperf.com/defined-function-vs-in-loop-function/3</a></p>
  
  <p>You will notice that this code:</p>
  
  <pre><code>for (var i = 0; i &lt; loops; i++) {
     function func(a, b) {
      return a + b;
    };
    func(i, i);
  }
  </code></pre>
  
  <p>Performs on par with this code:</p>
  
  <pre><code>function declaredFn(a, b) {
      return a + b;
  };
  
  
  for (i = 0; i &lt; loops; i++) {
      declaredFni, i);
  }
  </code></pre>
  
  <p>But this code:</p>
  
  <pre><code>for (i = 0; i &lt; loops; i++) {
    var func = function(a, b) {
      return a + b;
    };
    func(i, i);
  }
  </code></pre>
  
  <p>is significantly slower then this code:</p>
  
  <pre><code>var expfunc = function(a, b) {
    return a + b;
  };
  for (i = 0; i &lt; loops; i++) {  
    expfunc(i, i);
  }
  </code></pre>
  
  <p>Why? What is happening internally?</p>