reduce vs filter-count

Benchmark created on


Setup

const max = 10000

const list = Array.from(Array(max)).map(x=> Math.random())

const chosen = new Set([
	Math.floor(Math.random() * max),
	Math.floor(Math.random() * max),
	Math.floor(Math.random() * max),
	Math.floor(Math.random() * max),
	Math.floor(Math.random() * max),
	Math.floor(Math.random() * max),
	Math.floor(Math.random() * max),
	Math.floor(Math.random() * max),
	Math.floor(Math.random() * max),
	Math.floor(Math.random() * max),
	Math.floor(Math.random() * max),
	Math.floor(Math.random() * max),
	Math.floor(Math.random() * max),
	Math.floor(Math.random() * max)])

Test runner

Ready to run.

Testing in
TestOps/sec
red
const count = list.reduce((x,acc) => {if (chosen.has(x)) {acc += 1} return acc;}, 0)
ready
fil
const count = list.filter(x => chosen.has(x)).length
ready

Revisions

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