Reduce vs. loop (v21)

Revision 21 of this benchmark created by Peter Todd on


Setup

var arr = [];
  for (var i=0; i<50; i++) arr.push(Math.floor(Math.random() * 10000));

Test runner

Ready to run.

Testing in
TestOps/sec
loop (for .. of)
let sum = 0;
for (const v of arr) {
  sum += v;
}
ready
loop
let sum = 0;
for (let i = 0; i < arr.length; i++) {
  sum += arr[i];
}
ready
loop (for .. in)
let sum = 0;
for (const v in arr) {
  sum += v;
}
ready
Reduce
total = arr.reduce(function(curr_total, val) {
    return curr_total + val;
}, 0);
ready
forEach
let sum = 0;
arr.forEach(v => sum += v);
ready

Revisions

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