flatMap vs map + filter

Benchmark created on


Setup

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

Test runner

Ready to run.

Testing in WebKit 537.36 / undefined
TestOps/sec
flatMap
baseList.flatMap(valueObj => {
	if (valueObj.value > 0.5) {
		return valueObj.value
	}
	return []
})
ready
flatMap static empty
baseList.flatMap(valueObj => {
	if (valueObj.value > 0.5) {
		return valueObj.value
	}
	return EMPTY_ARRAY
})
ready
filter + map
baseList.filter(valueObj => 
  valueObj.value > 0.5
).map(valueObj => valueObj.value)
ready

Revisions

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