Compare loop vs reduce

Benchmark created on


Setup

const size = 1024;
const array = Array(size).fill(0);
for (let i = 0; i < size; ++i)
	array[i] = Math.random() * 1000;
	

Test runner

Ready to run.

Testing in
TestOps/sec
Simple loop i
let sum = 0;
for (let i = 0; i < size; ++i) sum += array[i];
ready
Reduce
const sum = array.reduce((a, b) => a + b, 0);
ready
forEach
let sum = 0;
array.forEach(item => sum += item);
ready
Simple loop of
let sum = 0;
for (const item of array) sum += item;
ready

Revisions

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