scope chain lookups

JavaScript performance comparison

Test case created by Ben Newman

Info

Is it worthwhile to alias variables from outer scopes using local variables in hot code?

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var outer = [];
   
    function useLocal(n) {
      var local = outer;
      local.length = 0;
      while (n --> 0) {
        local.push(n);
      }
      return local.length;
    }
   
    function useOuter(n) {
      outer.length = 0;
      while (n --> 0) {
        outer.push(n);
      }
      return outer.length;
    }
   
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Alias outer to a local variable
useLocal(100);
pending…
Use outer variable directly
useOuter(100);
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 comments

Add a comment