for vs forEach (v82)

Revision 82 of this benchmark created on


Setup

var i = 0,
    value = 0,
    length = 10000,
    values = [],
    sum = 0,
    context = values;
  
  
  for (i = 0; i < length; i++) {
    values[i] = Math.random();
  }
  
  function add(val) {
    sum += val;
  }

Test runner

Ready to run.

Testing in
TestOps/sec
forEach closure
values.forEach(function(val) {
  sum += val;
});
ready
while
i = 0
length = values.length
while(i < length){
  sum += values[i];
  i += 1;
}
ready
for ... in
for (i in values) {
  sum += values[i];
}
ready
forEach
values.forEach(add);
ready
for
for (i = 0; i < length; i += 1) {
  sum += values[i];
}
ready

Revisions

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