for vs forEach (v324)

Revision 324 of this benchmark created on


Description

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

Setup

var arr, values;
    
    arr = [];
    values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
    
    function pushToArr(val) {
      arr.push(val);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
forEach
values.forEach(pushToArr);
ready
for loop, simple
for (var i = 0; i < values.length; i++) {
  pushToArr(values[i]);
}
ready
for loop with closure
for (var i = 0; i < values.length; i++) {
  (function(val, i) {
    pushToArr(val);
  }(values[i], i));
}
ready

Revisions

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