forEach & reduce comparasion

Benchmark created on


Setup

let data = [];
for(let i =0;i<10000;i++){
	data.push({
		name:"Name"+i,
		age:Math.floor(Math.random()*100)
	})
}

Test runner

Ready to run.

Testing in
TestOps/sec
With forEach
let a ='';
let b =0;
data.forEach(value=>{
	a+=value.name;
	b+=value.age
})
ready
With reduce
const z = data.reduce((acc,curr)=>{
	acc.a+=curr.name;
	acc.b+=curr.age;
	return acc;
},{a:'',b:0})
ready

Revisions

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