for vs while vs forEach loop

JavaScript performance comparison

Revision 265 of this test case created by Krinkle

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var i,
    x = 0;
    list = [];
  
  
  for (i = 0; i < 2000; i++) {
    list[i] = Math.random();
  }

};
</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
while (reverse)
i = list.length;
while (i--) {
  x += list[i];
}
pending…
forEach
list.forEach(function(value) {
  x += value;
});
pending…
for
for (i = 0; i < list.length; i++) {
  x += list[i];
}
pending…
for w/ cached length
var len = list.length;
for (i = 0; i < len; i++) {
  x += list[i];
}
pending…
for (reverse)
for (i = list.length - 1; i >= 0; i--) {
  x += list[i];
}
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