For loop vs .reduce (v2)

Revision 2 of this benchmark created on


Setup


// Create a million random numbers
const values = new Array(1_000_000).fill(true).map(() => Math.random()); 

Test runner

Ready to run.

Testing in
TestOps/sec
for of loop
let result = 0; 

for(let i of values){
	result += i; 
}
ready
reduce
const result = values.reduce((acc,cur) => {
	return acc + cur; 
},0)
ready
for i
let result =0; 
for(let i =0; i<values.length; i++){
	const value = values[i]; 
	result += value; 
}
ready
.forEach
let value = 0; 
values.forEach((v)=> {
	value +=v; 
})
ready

Revisions

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