Native Map & Reduce vs For Loops

JavaScript performance comparison

Test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function generate(n) {
    var data = [];
    for (var i = 0; i < n; i++) data.push(i);
    return data;
  };
  
  var n = 10000;
  
  var data = generate(n);
  
  function map(i) {
    return i + 1;
  };
  
  function reduce(result, current) {
    return result == null ? current : result + current;
  };

};
</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
Array.map
return data.map(map);
pending…
For.map
var result = [];
for (var i = 0; i < n; i++) {
  result.push(map(data[i]));
}
return result;
pending…
Array.reduce
return data.reduce(reduce);
pending…
For.reduce
var result = null;
for (var i = 0; i < n; i++) {
  result = reduce(result, data[i]);
}
return result;
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