Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
flatmap vs reduce to filter undefined elements
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0
Browser:
Firefox 124
Operating system:
Mac OS X 10.15
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
flatMap
5827.2 Ops/sec
reduce
8954.1 Ops/sec
Script Preparation code:
const myArray = []; for (let i = 0; i < 10000; i++) { const obj = { prop1: `value${i}_1`, prop2: `value${i}_2`, prop3: `value${i}_3`, prop4: `value${i}_4`, }; // Randomly add the metadata property if (Math.random() < 0.5) { obj.metadata = { info1: `metadata${i}_1`, info2: `metadata${i}_2`, }; } myArray.push(obj); }
Tests:
flatMap
myArray.flatMap(obj => { if (obj.metadata) { return { prop1: obj.prop1, metadata: obj.metadata }; } else { return []; } })
reduce
myArray.reduce((acc, obj) => { if (obj.metadata) { acc.push({ prop1: obj.prop1, metadata: obj.metadata }); } return acc; }, []);