var arr = [];
var i = 0;
while (i <= 1E5) arr[i] = i++;
arr.filter(x => x % 3).map(x => x/100)
arr.flatMap(x => x % 3 ? x/100 : [])
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
filter map speed | |
flatMap speed |
Test name | Executions per second |
---|---|
filter map speed | 593.0 Ops/sec |
flatMap speed | 724.5 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Overview
The benchmark is designed to compare the performance of two different methods for transforming an array: Array.prototype.filter()
followed by Array.prototype.map()
, and Array.prototype.flatMap()
. The goal is to determine which method is faster for this specific use case.
Options Compared
There are two options being compared:
filter()
, then mapping each element to a new value using map()
.flatMap()
method, which is a more recent addition to the ECMAScript standard (introduced in ES6). flatMap()
combines the benefits of filter()
and map()
into a single method.Pros and Cons of Each Approach
flatMap()
(introduced in ES6).Library Used
In this benchmark, the library used is not explicitly mentioned. However, since both methods involve array operations, it can be inferred that the JavaScript engine being tested supports the ECMAScript standard.
Special JS Feature or Syntax
There are no special features or syntaxes being used in this benchmark. The focus is on comparing two existing methods for transforming arrays.
Other Alternatives
Alternative approaches to compare filter()
+ map()
with flatMap()
could include:
Array.prototype.filter()
vs. Array.prototype.flatMap()
.Array.prototype.map()
vs. Array.prototype.flatMap()
.filter()
+ map()
or flatMap()
with equivalent operations in a native language, such as C++.In this specific benchmark, the focus is on comparing the performance of two existing methods for transforming arrays, which makes it more focused and relevant to JavaScript developers.