for vs forEach (v323)

Revision 323 of this benchmark created on


Description

Is it faster to use the native forEach or just loop with for?

Preparation HTML

<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

Ready to run.

Testing in
TestOps/sec
forEach
values.forEach(addToStorage);
ready
for loop, simple
for (i = 0; i < values.length; i++) {
 addToStorage(values[i]);
}
ready
for loop, cached length
var len = values.length;
for (i = 0; i < len; i++) {
 addToStorage(values[i]);
}
ready
for loop, reverse
for (i = values.length - 1; i >= 0; i--) {
 addToStorage(values[i]);
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.