a=[];
for(i=0;i<1000;i++) a.push(Number(i)/1000);
var filtering=x=>(x*114514)%1>0.5;
var mapping=x=>x+0.1919;
var reducing=(acc,x)=>{
var value=mapping(x);
if(filtering(value)) acc.push(value);
return acc;
}
a.filter(filtering).map(mapping);
a.reduce(reducing,[]);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
map-filter | |
reduce |
Test name | Executions per second |
---|---|
map-filter | 42399.7 Ops/sec |
reduce | 39197.0 Ops/sec |
Let's break down the provided benchmark and explain what's being tested, compared, and considered.
Benchmark Definition
The benchmark consists of two test cases:
map-filter
: This test case compares the performance of two approaches to achieve the same result:a.filter(filtering).map(mapping)
reduce
)reduce
: This test case measures the performance of using Array.prototype.reduce()
to achieve the same result as map-filter
.Script Preparation Code
The script preparation code generates an array a
containing 1000 elements, each representing a fraction with a numerator between 1 and 1000 (inclusive) and a denominator of 1000. Two functions are also defined:
filtering(x)
: Returns true
if (x * 114514) % 1 > 0.5
, which is a simple filtering condition.mapping(x)
: Adds 0.1919 to the input value x
.The script preparation code sets up these functions and uses them to generate an array of filtered values using both approaches (map-filter
and reduce
). The resulting arrays are then pushed onto another array acc
.
Html Preparation Code
There is no HTML preparation code provided, which means that the benchmark does not test any rendering or UI-related performance.
Comparison of Approaches
The two approaches being compared in the map-filter
test case:
a.filter(filtering).map(mapping)
: This approach uses Array.prototype.filter()
to filter the elements, followed by Array.prototype.map()
to apply the mapping function. The filtered array is then mapped again using another instance of Array.prototype.map()
.reduce
)): This approach uses Array.prototype.reduce()
to accumulate the results.Pros and Cons
map-filter
):reduce
):Library Usage
The benchmark uses Array.prototype.filter()
, Array.prototype.map()
, and Array.prototype.reduce()
without any additional libraries. These are built-in methods on the Array prototype, making them accessible from anywhere in JavaScript code.
Special JS Feature or Syntax
This benchmark does not use any special features or syntax, such as async/await, generators, or modern ES6+ features like arrow functions or template literals.
Alternatives
If you're interested in exploring alternative approaches for this benchmark, consider the following:
Array.prototype.forEach()
: Instead of filtering and mapping using filter
and map
, use forEach()
to iterate over the array and apply both operations sequentially.Keep in mind that these alternatives may not provide the same results as the original benchmark, and their performance characteristics may vary depending on the specific use case and dataset.