Loop vs map vs forEach with functions only (v4)

Revision 4 of this benchmark created on


Setup

var range = [];
  
  for (var i = 0; i < 10000; i++) {
    range.push(i);
  }
  
  function multiplyByTwo(num) {
    return num * 2;
  }

Test runner

Ready to run.

Testing in
TestOps/sec
Loop
var result = [];

for (var i = 0, m = range.length; i < m; i++) {
  result.push(multiplyByTwo(range[i]));
}
ready
Map
var result = range.map(multiplyByTwo);
ready
forEach
var result = [];

range.forEach(function(val) {
  result.push(multiplyByTwo(val));
});
ready

Revisions

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