looping vs reduce vs map (v2)

Revision 2 of this benchmark created on


Setup

function generateRandomArray(length) {
  const arr = [];
  for (let i = 0; i < length; i++) {
    arr.push(Math.floor(Math.random() * 101));
  }
  return arr;
}
var myArray = generateRandomArray(10000);

Test runner

Ready to run.

Testing in
TestOps/sec
looping
let sum = 0;

for (let i = 0; i < myArray.length; i++) {
  sum += myArray[i];
}

ready
reduce
let sum = myArray.reduce((a, v) => a + v, 0);
ready
map
let sum = 0
myArray.map(e => sum += e);
ready

Revisions

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