Reduce thingy

Benchmark created on


Setup

const array = Array.from(new Array(10000000), () => ({
	a: Math.floor(Math.random() * 10000),
	b: Math.floor(Math.random() * 10000)
}))

Test runner

Ready to run.

Testing in
TestOps/sec
Reduce mutate
array.reduce((obj, item) => {
	obj.a += item.a;
	obj.b += item.b;

	return obj;
}, {a: 0, b: 0})
ready
Reduce
array.reduce((obj, item) => {
	return {
		a: obj.a + item.a,
		b: obj.a + item.b
	}
}, {a: 0, b: 0})
ready
Real FOR
let a = 0;
let b = 0;

for(let i = 0; i < array.length; i++) {
	a += array[i].a;
	b += array[i].b;
}
ready
(original tweet) - multireduce
const output = {
	a: array.reduce((sum, i) => sum + i.a, 0),
	b: array.reduce((sum, i) => sum + i.b, 0)
}
ready
(original tweet) - forEach
let a = 0;
let b = 0;

array.forEach(item => {
	a += item.a;
	b += item.b;
})
ready

Revisions

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