.filter.map vs .flatMap

Benchmark created on


Setup

const arr = [-1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6 -7, 7, -8, 8, -9, 9, -10, 10]

Test runner

Ready to run.

Testing in
TestOps/sec
.filter.map
const arr2 = arr.filter(x => x > 0).map(x => x * 2);
ready
.flatMap (array for empty)
const arr2 = arr.flatMap(x => x > 0 ? x * 2 : []);
ready
.flatMap (array for all)
const arr2 = arr.flatMap(x => x > 0 ? [x * 2] : []);
ready

Revisions

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