flatMap vs filter and map (v6)

Revision 6 of this benchmark created on


Description

exploring whether intermediary array costs can be circumvented.

Setup

const persons = Array.from({ length: 1_000_000 }, (_, i) => ({
  name: `Person${i}`,
  age: Math.floor(Math.random() * 100)
}));

Test runner

Ready to run.

Testing in
TestOps/sec
Filter and map
persons.filter(p => p.age > 60).map(p => p.name);
ready
flatMap
persons.flatMap(({ age, name }) => age > 60 ? [name] : []);
ready
flatMap2
const dummy = []

persons.flatMap(({ age, name }) => age > 60 ? name : dummy);
ready
flatMap3


persons.flatMap(({ age, name }) => age > 60 ? name : []);
ready
Reduce
persons.reduce((memo, p) => {
  if (p => p.age > 60) {
    memo.push(p.name)
  }
  return memo;
}, []);
ready

Revisions

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