Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
filter-map vs reduceggg
(version: 0)
modified version of `map-filter vs reduce` that switches the order of operations
Comparing performance of:
map-filter vs reduce
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
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)) { return [...acc, value]; }; return acc; }
Tests:
map-filter
a.filter(filtering).map(mapping);
reduce
a.reduce(reducing,[]);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
map-filter
reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36
Browser/OS:
Chrome 139 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
map-filter
42387.0 Ops/sec
reduce
7782.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark is defined by two JavaScript expressions: `a.filter(filtering).map(mapping)` and `a.reduce(reducing,[])`. These expressions are designed to simulate a real-world scenario where data is filtered, mapped, and reduced (i.e., aggregated). **Options Compared** Two options are being compared: 1. **Filter-Map**: The first expression, `a.filter(filtering).map(mapping)`, applies filtering to the data first and then maps each result. 2. **Reduce**: The second expression, `a.reduce(reducing,[])`, applies reduction (aggregation) directly to the original data. **Pros and Cons** * **Filter-Map**: + Pros: This approach can be more efficient for small datasets or when filtering is a critical step before mapping. + Cons: This approach can lead to slower performance for large datasets due to the additional overhead of filtering before mapping. * **Reduce**: + Pros: This approach can be faster for large datasets since it avoids the overhead of filtering and maps each element directly. + Cons: This approach may not be suitable when filtering is a critical step, as it requires passing the entire dataset through the reduction function. **Library Usage** There is no explicit library mentioned in the provided JSON. However, the `filter`, `map`, and `reduce` methods are part of the built-in JavaScript API. **Special JS Features or Syntax** None are explicitly mentioned in the provided JSON. However, it's worth noting that the use of arrow functions (`=>`) for defining small anonymous functions is a modern JavaScript feature introduced in ECMAScript 2015 (ES6). **Benchmark Preparation Code** The code snippet prepares an array `a` with 1000 elements and defines three functions: 1. `filtering(x)`: Returns `true` if `(x*114514)%1>0.5`. 2. `mapping(x)`: Adds `0.1919` to the input value `x`. 3. `reducing(acc, x)`: Applies the `mapping` function to `x`, then filters based on the result using the `filtering` function. If filtered out, returns `acc` (the accumulator); otherwise, adds the mapped value to `acc`. **Other Alternatives** If you were to implement this benchmark without using built-in JavaScript methods, you could consider alternatives like: 1. Using a custom filtering and mapping loop. 2. Implementing the reduction logic manually using loops. 3. Utilizing a library like Lodash, which provides similar utility functions for filtering, mapping, and reducing arrays. Keep in mind that these alternatives would likely be less efficient than using built-in JavaScript methods.
Related benchmarks:
filter-map vs reduce
filter-map vs reduce vs reduce with destructuring
filter-map vs reduce fixed
filter-map vs reduce 2
filter-map vs reduce 100k
Comments
Confirm delete:
Do you really want to delete benchmark?