local var vs this.

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var F = function() {};
    F.prototype.foo = 20;
    F.prototype.local = function() {
      var bar = this.foo;
      for (var i = 0; i < 10000; i++) {
        bar += i >> 8;
      }
      this.foo = bar;
    }
   
    F.prototype.scoped = function() {
      for (var i = 0; i < 10000; i++) {
        this.foo += i >> 8;
      }
    }
   
    var inst = new F();
};

Benchmark.prototype.teardown = function() {
    inst.foo = 20;
};
</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
local
inst.local();
pending…
this
inst.scoped();
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment