Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
filter-map vs reduce 2
(version: 0)
modified version of `map-filter vs reduce` that switches the order of operations
Comparing performance of:
map-filter vs reduce
Created:
4 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:
10 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
map-filter
39697.1 Ops/sec
reduce
6652.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its options. **Benchmark Definition** The benchmark is called "filter-map vs reduce 2" and represents a modified version of the classic "map-filter vs reduce" benchmark. The script preparation code defines an array `a` with 1000 elements, each containing a number divided by 1000. Three functions are defined: * `filtering(x)`: takes a value `x` and returns whether it satisfies the condition `(x * 114514) % 1 > 0.5`. * `mapping(x)`: takes a value `x` and returns `x + 0.1919`. * `reducing(acc, x)`: takes an accumulator `acc` and a value `x`, applies the `mapping` function to `x`, checks if it satisfies the condition using `filtering`, and if so, appends the mapped value to the accumulator. **Options Compared** The benchmark compares two approaches: 1. `a.filter(filtering).map(mapping)`: This approach first filters the array `a` using the `filtering` function, then maps each element to a new value using the `mapping` function. 2. `a.reduce(reducing, [])`: This approach reduces the array `a` using the `reducing` function as the reduction callback. **Pros and Cons of Each Approach** **1. `a.filter(filtering).map(mapping)`** * Pros: + Can be more efficient for small arrays since filtering and mapping are applied sequentially. + Allows for easy debugging and understanding of the intermediate results. * Cons: + May be slower for large arrays due to the overhead of creating an intermediate array. **2. `a.reduce(reducing, [])`** * Pros: + Can be more efficient for large arrays since it uses a single pass through the data. + Can take advantage of optimizations provided by the reducer function (e.g., early return conditions). * Cons: + May be less intuitive to understand for those unfamiliar with the reducer pattern. **Library Used** The benchmark uses the `Array.prototype.filter()` and `Array.prototype.map()` methods, which are built-in JavaScript methods. There is no external library used in this benchmark. **Special JS Feature or Syntax** There are no special JavaScript features or syntaxes used in this benchmark. It only relies on standard JavaScript constructs such as functions, loops, and array methods. **Other Alternatives** If you were to rewrite this benchmark with alternative approaches, here are some options: * Use a library like `lodash` that provides its own filtering and mapping functions (e.g., `_.filter()` and `_.map()`) for potentially improved performance. * Implement the filtering and mapping operations manually using bitwise operators or other low-level techniques. * Compare the benchmark with a different reduction algorithm, such as `reduceRight()` or a custom implementation of the reducer function. Keep in mind that these alternatives may not necessarily produce better results and should be evaluated based on their own trade-offs.
Related benchmarks:
filter-map vs reduce
filter-map vs reduce vs reduce with destructuring
filter-map vs reduce fixed
filter-map vs reduce 100k
Comments
Confirm delete:
Do you really want to delete benchmark?