for loop vs Array.prototype.map bench

JavaScript performance comparison

Test case created by pakastin

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var data = new Array(10000);
  
  for (var i = 0; i < data.length; i++) {
    data[i] = i;
  }

};
</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
var multipliedData = new Array(data.length);

for (var i = 0; i < data.length; i++) {
  multipliedData[i] = data[i] * 2;
}
pending…
map
var multipliedData = data.map(function (item) {
  return item * 2;
});
pending…
for-loop-non-preallocated
var multipliedData = [];

for (var i = 0; i < data.length; i++) {
  multipliedData.push(data[i] * 2);
}
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