Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
reduce vs filter 22
(version: 0)
reduce vs filter 22
Comparing performance of:
Reduce vs Filter
Created:
3 years ago
by:
Guest
Go to the latest result
Script Preparation code:
var map = {}; for (let i = 0; i < 20000; i++) { map[`${i}`] = (i % 2) === 0; }
Tests:
Reduce
Object.keys(map).reduce((arr, key) => { if (map[key]) { arr.push(key); } return arr; }, []);
Filter
Object.keys(map).filter((key) => map[key]);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Reduce
Filter
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Filter
2K/s
Reduce
2K/s
View exact numbers
Test name
Executions per second
✓
Filter
1,813 Ops/sec
Reduce
1,592 Ops/sec
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark compares the performance of two JavaScript methods: `reduce` and `filter`, both used to extract even-indexed keys from a predefined object (`map`). Let's break down each approach: **1. Reduce:** * **Benchmark Definition:** `Object.keys(map).reduce((arr, key) => { if (map[key]) { arr.push(key); } return arr; }, []);` * This method iterates over the keys of the `map` object using `Object.keys(map)`. * For each key (`key`), it evaluates if its corresponding value in `map[key]` is true (indicating an even index). If true, the key is pushed into an array (`arr`). The `reduce` method returns this final array containing only even-indexed keys. **2. Filter:** * **Benchmark Definition:** `Object.keys(map).filter((key) => map[key]);` * This method directly filters the keys of the `map` object. * It iterates over each key and uses a callback function `(key) => map[key]` to check if its corresponding value in `map` is true. Only keys that satisfy this condition are included in the resulting array. **Pros and Cons:** * **Reduce:** More flexible as it allows for custom logic within the callback function, enabling more complex transformations beyond simple filtering. However, it might be less concise than `filter` for straightforward filtering tasks. * **Filter:** More concise and readable for basic filtering operations. It is optimized specifically for creating a new array with elements that meet a certain condition. **Other Considerations:** * The benchmark only focuses on execution speed. In real-world scenarios, readability, maintainability, and the specific use case might influence which method is more suitable. * Both methods iterate over the keys of the `map` object. If `map` were significantly larger, this iteration could become a performance bottleneck regardless of the chosen method. **Alternatives:** * A for loop with conditional checks could be used to achieve similar results to both `reduce` and `filter`, potentially offering fine-grained control but often being less readable.
Related benchmarks
filter-map vs reduce
filter-map vs reduce vs reduce with destructuring
filter-map vs reduce 2
filter-map vs reduce 100k
Comments
Confirm delete:
Do you really want to delete benchmark?