flatMap vs concat filter map

Benchmark created on


Setup

const baseList = Array.from({length: 1000}, (_, idx) => ({value: Math.random(), arrayValues: [Math.random(), Math.random()]}))

Test runner

Ready to run.

Testing in
TestOps/sec
filter + map + concat
[].concat(...baseList.filter((obj) => 
  obj.value > 0.5
).map((obj) => 
  obj.arrayValues
))
ready
flatMap
baseList.flatMap((obj) => {
  if (obj.value > 0.5) {
  	return obj.arrayValues
  }
  return []
})
ready

Revisions

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