this or var

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function varFunc() {
      var a = 2;
      return a*a*a;
    }
   
    this.a = 2;
    function thisFunc() {
      return this.a * this.a * this.a;
    }
   
    function cachedThisFunc() {
      var a = this.a;
      return a * a * a;
    }
   
    function varObjFunc() {
      var obj = { a: 2 };
      return obj.a * obj.a * obj.a;
    }
   
    function cachedVarObjFunc() {
      var obj = { a: 2 };
      var a = obj.a;
      return a * a * a;
    }
   
};
</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 variable
varFunc();
pending…
object property
thisFunc();
pending…
cached object property
cachedThisFunc();
pending…
property of local object
varObjFunc();
pending…
cached property of local object
cachedVarObjFunc();
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