Array iteration

Benchmark created on


Setup

const array = Array.from({length: 100_000}, (_, i) => i);

Test runner

Ready to run.

Testing in
TestOps/sec
Conventional for loop
let total = 0;
for(let i = 0; i < array.length; i ++){
	total += (array[i] * array[i]);
}
console.log(total);
ready
Conventional for loop with optimization
let total = 0;
const length = array.length;
for(let i = 0; i < length; i ++){
	total += (array[i] * array[i]);
}
console.log(total);
ready
For of loop
let total = 0;
for(const element of array){
	total = total + (element ** 2);
}
console.log(total);
ready
For of + map
let total = 0;
for(const element of array.map(x => x ** 2)){
	total = total + element;
}
console.log(total);
ready
Map + reduce
console.log(array.map(x => x ** 2).reduce((a, b) => a + b));
ready
Reduce
console.log(array.reduce((acc, a) => acc + a ** 2));
ready
Reduce alt
console.log(array.reduce((acc, a) => acc + a * a));
ready

Revisions

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