for loop vs Array.prototype.map()

JavaScript performance comparison

Test case created by James Hibbard

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var arr = Array.apply(null, Array(1000)).map(Number.prototype.valueOf, 0);
  var newArr = [];
  
  function addOne(val) {
    return val + 1;
  }

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
for loop
for (var i = 0, len = arr.length; i < len; i++) {
  newArr.push(addOne(arr[i]));
}
pending…
map
newArr = arr.map(addOne)
pending…

Compare results of other browsers

Revisions

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

0 Comments