this versus local variable

JavaScript performance comparison

Test case created by Jorasso

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function Item1() {
      this.test = Math.random(); //N = random value;
    }
   
    Item1.prototype.f = function() {
      var a = this.test * 10;
      var b = this.test * 20;
      var c = this.test * 30;
    }
   
    function Item2() {
      this.test = Math.random(); //N = random value;
    }
   
    Item2.prototype.f = function() {
      var test = this.test;
      var a = test * 10;
      var b = test * 20;
      var c = test * 30;
    }
   
    var thisInstance = new Item1();
    var localInstance = new Item2();
};
</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
This
for (var i = 0; i < 1000; i++) {
  thisInstance.f();
}
pending…
Local
for (var i = 0; i < 1000; i++) {
  localInstance.f();
}
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