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)=>{
if((x*114514)%1>0.5) acc.push(x+0.1919);
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 | 31658.0 Ops/sec |
reduce | 34058.5 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Definition
The benchmark is defined by two test cases:
a.filter(filtering).map(mapping);
(Test Name: "map-filter")a.reduce(reducing,[]);
(Test Name: "reduce")These two test cases are being compared to see which one performs better.
Options Compared
The options being compared are the order of operations in the two test cases:
Pros and Cons of Different Approaches
Library Used
None of the provided benchmark test cases use any external libraries. However, the script preparation code uses built-in JavaScript functions:
filter()
: Removes all elements from an array that don't match the condition specified by a function.map()
: Creates a new array with the results of applying a provided function on every element in this array.reduce()
: Applies a user-supplied function to each element in an array (reducing it to a single value).Special JS Feature or Syntax
The script preparation code uses regular expressions (x=>(x*114514)%1>0.5;
), but there is no special JavaScript feature or syntax being tested.
Other Alternatives
For benchmarking similar operations, alternatives could include:
forEach()
instead of filter()
.In conclusion, the provided benchmark compares two common JavaScript operations: filtering and mapping. The options being tested are the order of these operations, and the test cases aim to highlight any potential differences in performance between sequential vs parallel processing approaches.