Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
filter-map vs reduce, fixed.
(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)=>{ if((x*114514)%1>0.5) acc.push(x+0.1919); 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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The provided JSON represents a JavaScript microbenchmark that compares two approaches: `filter-map` and `reduce`. The benchmark is modified to switch the order of operations, which makes it a variation of the original "map-filter vs reduce" benchmark. **Script Preparation Code** The script preparation code defines three functions: 1. `filtering(x)`: takes an input `x`, multiplies it by 114514, and checks if the result is greater than 0.5 modulo 1 (i.e., a non-integer value). If true, returns whether the original value plus 0.1919 is greater than 0.5. 2. `mapping(x)`: takes an input `x` and returns the result of adding 0.1919 to it. 3. `reducing(acc, x)` : accumulates values in `acc`, pushing new values when the filtering condition is met. **Html Preparation Code** There is no HTML preparation code provided, which means that the benchmark does not take into account any DOM-related overhead or asynchronous operations. **Options Compared** The two options being compared are: 1. **Filter-Map**: This approach uses `Array.prototype.filter()` to filter out elements that don't meet the filtering condition, followed by `Array.prototype.map()` to apply the mapping function to the remaining elements. 2. **Reduce**: This approach uses `Array.prototype.reduce()` to accumulate values in an accumulator array, applying the reduction function to each element in turn. **Pros and Cons** 1. **Filter-Map** * Pros: + Easier to understand for developers familiar with the `filter()` and `map()` methods. + Can be more intuitive for developers who are already comfortable with using these methods. * Cons: + May incur overhead due to the additional method calls (two operations: filtering and mapping). 2. **Reduce** * Pros: + More efficient, as it avoids the overhead of separate `filter()` and `map()` method calls. + Can be more effective for accumulating values in an array, especially when dealing with large datasets. * Cons: + May require more understanding of the reduction function and its side effects (e.g., modifying the accumulator). **Library: Lodash** The `reduce` option uses the `reduce()` method from the Lodash library. This is a popular utility library that provides additional functional programming helpers, including `reduce()`. The use of Lodash here allows for a more concise implementation of the reduction function. **Special JS Feature or Syntax** There are no special JavaScript features or syntax being used in this benchmark. **Other Considerations** The benchmark measures the number of executions per second (ExecutionsPerSecond) for each browser, which provides a relative performance comparison. However, keep in mind that this measurement can be influenced by various factors, such as: * Browser version and configuration * System resources (CPU, memory) * Network latency and overhead **Alternatives** If you're interested in exploring alternative approaches or libraries, here are some options to consider: 1. **Array.prototype.every()` instead of `filter()`: This method returns a boolean indicating whether all elements in the array pass the test. 2. **Array.prototype.forEach()` with custom filtering: Instead of using `filter()` and then `map()`, you could use `forEach()` and apply custom filtering logic within the callback function. 3. **Underscore.js or other libraries**: Explore alternative utility libraries that provide similar functionality to Lodash, such as `underscore.js` or `ramda`.
Related benchmarks:
filter-map vs reduce
filter-map vs reduce vs reduce with destructuring
filter-map vs reduce fixed
filter-map vs reduce 2
Comments
Confirm delete:
Do you really want to delete benchmark?