closures

JavaScript performance comparison

Test case created by roy

Info

This is a test to try to see the speed of execution of two ways of make a program, one that access variables in a scope, and one that access variables passed in a closure

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var someVar = {
      someProp: {
        someOtherProp: 'someValue'
      }
    };
   
    var fn = function(val) {};
};
</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
accessing the global values
(function() {
  var someValue = someVar.someProp.someOtherProp;
  (function() {
    var someValueWillBeUsed = someValue;
    (function() {
      var someValueWillBeUsed = someValue;
      for (var i = 0; i < 10000; i++) {
        fn(someValueWillBeUsed);
      }
    }());
  }());
})();
pending…
passing the global variable in outer closure
(function(someValue) {
  (function() {
    var someValueWillBeUsed = someValue;
    (function() {
      var someValueWillBeUsed = someValue;
      for (var i = 0; i < 10000; i++) {
        fn(someValueWillBeUsed);
      }
    }());
  }());
})(someVar.someProp.someOtherProp);
pending…
passing the global variable internally
(function(someValue, f2) {
  var someValueWillBeUsed = someValue;
  (function(sv, f3) {
    var someValueWillBeUsed = someValue;
    for (var i = 0; i < 10000; i++) {
      f3(someValueWillBeUsed);
    }
  }(someValue, f2));
}(someVar.someProp.someOtherProp, fn));
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