for loop vs Array.prototype.map() (v9)

Revision 9 of this benchmark created by Corey Hart on


Setup

var arr = [1, 2, 3];
    while (arr.length < 1000) {
      arr.push.apply(arr, arr);
    }
    
    function addOne(val) {
      return val + 1;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
For Loop
var i = -1,
  l = arr.length,
  results = new Array(l);

for (; ++i < l;) {
  results[i] = arr[i] + 1;
}
ready
Map Cached
var results = arr.map(addOne)
ready
Map Inline
var results = arr.map(function(val) {
  return val + 1;
})
ready
For Loop Push
var results = [],
  i = -1,
  l = arr.length;

for (; ++i < l;) {
  results.push(arr[i] + 1);
}
ready

Revisions

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