Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
.filter().map() vs flatMap() vs reduce()
Compare performance of `.filter().map()`, `flatMap()`, and `reduce()` in JavaScript
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/137.0.0.0 Safari/537.36
Browser:
Chrome 137
Operating system:
Mac OS X 10.15.7
Device Platform:
Desktop
Date tested:
11 months ago
Test name
Executions per second
.filter().map()
1154.1 Ops/sec
.flatMap()
1055.9 Ops/sec
.reduce()
1768.5 Ops/sec
for ... of
1554.9 Ops/sec
regular for
2663.4 Ops/sec
regular for, prefilled then trimmed
578.5 Ops/sec
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
const data = Array.from({length: 2**16}, (_, i) => ({index: i}));
Tests:
.filter().map()
const filteredData = data .filter(i => i.index % 2 === 0) .map(i => i.index);
.flatMap()
const filteredData = data.flatMap(i => i.index % 2 === 0 ? [i.index] : []);
.reduce()
const filteredData = data.reduce((memo, i) => { if (i.index % 2 === 0) { memo.push(i.index); } return memo; }, []);
for ... of
const filteredData = []; for (let i of data) { if (i.index % 2 === 0) { filteredData.push(i.index); } }
regular for
const filteredData = []; for (let i = 0; i < data.length; i++) { if (data[i].index % 2 === 0) { filteredData.push(data[i].index); } }
regular for, prefilled then trimmed
let filteredData = Array.from({length: data.length}); let filteredIndex = 0; for (let i = 0; i < data.length; i++) { if (data[i].index % 2 === 0) { filteredData[filteredIndex++] = data[i].index; } } filteredData = filteredData.splice(filteredIndex, data.length - filteredIndex);