Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
Run results for:
flatMap() vs filter().map() vs reduce() vs forEach()
Test four different approaches for a combination of filtering and mapping elements of an array.
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36
Browser:
Chrome 119
Operating system:
Linux
Device Platform:
Desktop
Date tested:
2 years ago
Benchmark engine:
Benchmark.js
forEach
1K/s
reduce
848/s
filter().map()
678/s
flatMap()
257/s
View exact numbers
Test name
Executions per second
✓
forEach
1,075 Ops/sec
reduce
848 Ops/sec
filter().map()
678 Ops/sec
flatMap()
257 Ops/sec
Script Preparation code:
var arr = []; var i = 0; while (i <= 1E5) arr[i] = i++;
Tests:
filter().map()
arr.filter(x => x % 3).map(x => x/100)
flatMap()
arr.flatMap(x => x % 3 ? [x/100] : [])
reduce
arr.reduce((result, x) => { if (x % 3) result.push(x / 100); return result; }, [])
forEach
var result = []; arr.forEach(x => { if (x % 3) result.push(x / 100); });