for vs forEach (v325)

Revision 325 of this benchmark created by jkutianski on


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
for pop
for (var value; value = values.pop();) {
  addToStorage(value);
}
ready

Revisions

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