for vs forEach (v284)

Revision 284 of this benchmark created by Nathan on


Description

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

Preparation HTML

<script>
var values = new Array(10000);
var i, sum;

for (i = 0; i < 10000; i++) {
  values[i] = i % 3;
}
  
function add(val) {
  'use strict';
  sum += val;
}
</script>

Setup

sum = 0;

Test runner

Ready to run.

Testing in
TestOps/sec
forEach
values.forEach(add);
ready
for loop
for (i = 0; i < values.length; i++) {
  add(values[i]);
}
ready

Revisions

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