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

Revision 8 of this benchmark created by bfricka on


Description

For some reason current Chrome is deopting when using push, so we're seeing really bad tests (+/- 30% margins = bad test).

Adding back push for comparison. Also expected inlining for comparison. I'm not sure why the VM isn't able to do this inlining for the extremely simple addOne function, but clearly it's not.

Setup

for (var arr = [], arrLen = 1000; arrLen; arrLen--) arr.push(0);
    var newArr = Array(arr.length);
    
    function addOne(val) {
      return val + 1;
    }

Teardown


    arr = arrLen = newArr = addOne = null;
  

Test runner

Ready to run.

Testing in
TestOps/sec
for loop
for (var i = 0, len = arr.length; i < len; i++) {
  newArr[i] = addOne(arr[i]);
}
ready
map
newArr = arr.map(addOne)
ready
push
for (var i = 0, len = arr.length; i < len; i++) {
  newArr.push(addOne(arr[i]));
}
ready
Expected inlining assignment
for (var i = 0, len = arr.length; i < len; i++) {
  newArr[i] = arr[i] + 1;
}
ready
Expected inlining push
for (var i = 0, len = arr.length; i < len; i++) {
  newArr.push(arr[i] + 1);
}
ready

Revisions

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