for vs forEach

JavaScript performance comparison

Revision 325 of this test case created by jkutianski

Preparation code

<script>
  var i, values = [],
      storage = {};
  for (i = 0; i < 50; i++) {
   values[i] = "model" + i;
  }
  
  function addToStorage(val) {
    if (!(val in storage)) {
      storage[val] = { name: val };
    }
  }
</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
forEach
values.forEach(addToStorage);
pending…
for loop, simple
for (i = 0; i < values.length; i++) {
  addToStorage(values[i]);
}
pending…
for loop, cached length
var len = values.length;
for (i = 0; i < len; i++) {
  addToStorage(values[i]);
}
pending…
for loop, reverse
for (i = values.length - 1; i >= 0; i--) {
  addToStorage(values[i]);
}
pending…
for pop
for (var value; value = values.pop();) {
  addToStorage(value);
}
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