Closures And For Loop

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var f; // resulted function saved into this var
    // instead of passing into setTimeout
};
</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
Test1 20 function objects
for (var i = 0; i < 10; i++) {
  (function() {
    var i2 = i;
    f = function() {
      console.log(i2);
    };
  })();
}
pending…
Tes2 20 function objects
for (var i = 0; i < 10; i++) {
  f = (function(i2) {
    return function() {
      console.log(i2);
    };
  })(i);
}
pending…
Test3 11 function objects
function fGenerator(i2) {
  return function() {
    console.log(i2);
  }
}
for (var i = 0; i < 10; i++) {
  f = fGenerator(i);
}
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