Reduce vs. loop (v10)

Revision 10 of this benchmark created on


Setup

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

Test runner

Ready to run.

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

Revisions

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