fitlter x2 vs reduce

Benchmark created on


Setup

const arr = Array.from({length: 10000}).map((_, key) => key);

const shuffled = arr.sort(() => Math.random() - Math.random());

Test runner

Ready to run.

Testing in
TestOps/sec
double filter
const odd = shuffled.filter(n => n % 2 === 1);
const even = shuffled.filter(n => n % 2 === 0);

console.log(odd.length, even.length)
ready
reduce
const { odd, even } = shuffled.reduce((acc, n) => {
  const isOdd = n % 2 === 1;
  if(isOdd) {
  	acc.odd.push(n);
  } else {
  	acc.even.push(n)
  }
  
  return acc;
}, { odd:[], even: []})

console.log(odd.length, even.length);
ready

Revisions

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