Reduce vs. loop

JavaScript performance comparison

Revision 8 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var keyValuePairs= [];
  for (var i=0; i<50; i++) {
    keyValuePairs.push(['_'+i, Math.floor(Math.random() * 10000)]);
  }

};
</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
Reduce
var result = arr.reduce(function(obj, pair) {
    obj[pair[0]] = pair[1]; return objl
}, {});
pending…
Reduce w/arrow function
var result = arr.reduce((obj, pair) => {
    obj[pair[0]] = pair[1]; return objl
}, {});
pending…
loop
var result = {};
for (var i = 0, i < keyValuePairs.length; i++) {
  result[keyValuePairs[i][0]] = keyValuePairs[i][1];
}
pending…
loop (cache length)
var result = {};
for (var i = 0, var l = keyValuePairs.length; i < l; i++) {
  result[keyValuePairs[i][0]] = keyValuePairs[i][1];
}
pending…
loop (cache length, fetch pair)
var result = {};
for (var i = 0, var l = keyValuePairs.length; i < l; i++) {
  var pair = keyValuePairs[i]
  result[pair[0]] = pair[1];
}
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