Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Filtering and mapping with .filter(...).map(...) vs .flatMap(...) vs custom filterMap(...)
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0
Browser:
Chrome 126
Operating system:
Mac OS X 10.15.7
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
.filter.map
3703176.2 Ops/sec
.flatMap
863757.2 Ops/sec
filterMapEntries
2748371.0 Ops/sec
filterMapIndices
5370223.5 Ops/sec
Script Preparation code:
var arr = new Array(100).fill(null).map((val, index) => index); function filterMapEntries( arr, filterFn, mapFn, ) { const result = []; for (const [i, value] of arr.entries()) { if (filterFn(value)) { result.push(mapFn(value, i)); } } return result; } function filterMapIndices( arr, filterFn, mapFn, ) { const result = []; for (let i = 0; i < arr.length; i += 1) { const value = arr[i]; if (filterFn(value)) { result.push(mapFn(value, i)); } } return result; }
Tests:
.filter.map
const a = arr .filter((val) => val % 2 === 0) .map((val) => val + 10);
.flatMap
const a = arr.flatMap((val) => val % 2 === 0 ? [val + 10] : [])
filterMapEntries
const a = filterMapEntries(arr, (val) => val % 2 === 0, (val) => val + 10);
filterMapIndices
const a = filterMapIndices(arr, (val) => val % 2 === 0, (val) => val + 10);