filter + map vs reduce vs for loop (v2)

Revision 2 of this benchmark created on


Setup

const input = Array.from(Array(1000).keys())

Test runner

Ready to run.

Testing in
TestOps/sec
filter + map
const output = input
	.filter((i) => i % 2 === 0)
	.map((i) => i * 2)
ready
reduce
const output = input
	.reduce((acc, i) => {
		if (i % 2 === 0) acc.push(i * 2)
		return acc
	}, [])
ready
for loop
const output = []
for (let i = 0; i < input.length; i++) {
	if (i % 2 !== 0) continue
	output.push(i * 2)
}
ready

Revisions

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