Array loop iteration style

JavaScript performance comparison

Revision 2 of this test case created by Mihai Parparita

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var a = []
  for (var i = 0; i < 10000; i++) {
    a.push({p: i});
  }

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Index, no cached length
for (var i = 0; i < a.length; i++) {
  a[i].p++;
}
pending…
Index, cached length
var length = a.length;
for (var i = 0; i < length; i++) {
  a[i].p++;
}
pending…
End on undefined
for (var i = 0, p; p = a[i]; i++) {
  p.p++;
}
pending…
End on undefined, inline plusplus
for (var i = 0, p; p = a[i++];) {
  p.p++;
}
pending…
forEach
a.forEach(function(p) {
  p.p++;
});
pending…
Index, no cached length, variable
for (var i = 0; i < a.length; i++) {
  var p = a[i];
  p.p++;
}
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.

0 Comments