reduce is bad?

Benchmark created on


Setup

const x = [...Array(10_000).keys()].map(Math.random);

Test runner

Ready to run.

Testing in
TestOps/sec
reduce
const grouped = x.reduce((acc, cur) => {
	const label = acc < 0.5 ? "under" : "over";
	return {
		...acc, [label]: (acc[label] ?? 0) + 1
	}
}, {});
ready
for loop
const grouped = {};
for (const current of x) {
	const label = current < 0.5 ? "under" : "over";
	grouped[label] = (grouped[label] ?? 0) + 1;
}
ready

Revisions

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