var arr = Array.from({length:10000}, (v, i) => ({name: i, assigned: Math.random() < 0.5}));
arr.flatMap((o) => (o.assigned ? [o.name] : []));
arr.reduce((a, o) => (o.assigned && a.push(o.name), a), [])
{
const a = [];
for (const o of arr) if (o.assigned) a.push(o.name);
}
arr.filter(item => item.assigned).map(item => item.name)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
flatMap | |
reduce with push | |
loop with push | |
filter with a map |
Test name | Executions per second |
---|---|
flatMap | 3408.4 Ops/sec |
reduce with push | 10756.0 Ops/sec |
loop with push | 10914.1 Ops/sec |
filter with a map | 5866.6 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks!
Overview
MeasureThat.net provides a platform for creating and running JavaScript microbenchmarks. The benchmark you've provided consists of four test cases: flatMap
, reduce with push
, loop with push
, and filter with a map
. These test cases are designed to compare the performance of different approaches in filtering and mapping arrays.
Test Cases
Let's analyze each test case:
flatMap
: This test case uses the Array.prototype.flatMap()
method, which flattens an array by mapping over it and returning a new array with the mapped values.reduce with push
: This test case uses the Array.prototype.reduce()
method, which applies a reduction function to each element of an array (in this case, filtering out elements based on the presence of a specific property) and returns the accumulated result by pushing the filtered elements into an accumulator array.loop with push
: This test case uses a traditional for...of
loop with a conditional statement to filter out elements based on their presence in the assigned
property, and pushes the matching elements into an accumulator array.filter with a map
: This test case uses the Array.prototype.filter()
method to filter out elements based on their presence in the assigned
property, and then applies another mapping function (map()
) to extract the name
property from each filtered element.Options Compared
The main options compared in these test cases are:
flatMap
flattens an array by mapping over it, while filter with a map
maps over an already flattened array.reduce with push
uses a reduction function to accumulate the filtered elements into an accumulator array, whereas loop with push
pushes the filtered elements directly into an accumulator array.Pros and Cons
Here's a brief summary of the pros and cons for each approach:
Library Usage
There is no explicit library usage in these test cases, as they rely solely on built-in JavaScript methods (e.g., Array.prototype.flatMap()
, Array.prototype.reduce()
, etc.).
Special JS Features/Syntax
None of the test cases explicitly utilize any special JavaScript features or syntax beyond the standard array methods mentioned above.
Now that we've analyzed the benchmark, let's discuss alternatives:
Array.prototype.filter()
and then applying a custom function or method (e.g., map()
, forEach()
, etc.).Set
or Map
could be used for filtering and mapping operations.Keep in mind that benchmarking JavaScript performance can be complex and influenced by many factors. This analysis provides a general understanding of the test cases but may not cover all possible scenarios or edge cases.