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
Jump 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
Test name
Executions per second
Reduce
1591.8 Ops/sec
Filter
1812.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.1:latest
, generated one year ago):
Let's dive into the explanation of this benchmark. **What is being tested?** The test case compares the performance of two JavaScript methods: `reduce()` and `filter()`. These methods are used to manipulate arrays in JavaScript. **What options are compared?** In this specific test case, both methods are used on an object (`map`) that contains 20,000 key-value pairs. The task is to extract all keys from the object where the corresponding value is truthy (i.e., `true`). The two methods being tested are: 1. **Reduce()**: This method combines all elements of an array into a single output value. In this case, it's used with a callback function that iterates over the keys of the `map` object and pushes the key to an array if the corresponding value is truthy. 2. **Filter()**: This method creates a new array with all elements from the original array that pass the test implemented by the provided function. In this case, it's used with a callback function that iterates over the keys of the `map` object and returns the key if the corresponding value is truthy. **Pros and cons of different approaches** * **Reduce()**: This approach can be more efficient when you need to perform some aggregation or transformation on the elements, as it allows for accumulation of values. However, in this specific case, where we only need to filter the keys, it might not be the most suitable choice. * **Filter()**: This approach is generally more straightforward and easier to understand when you simply need to filter out certain elements. It creates a new array with the filtered elements, which can be beneficial if you need to keep the original order. **Library usage** There are no external libraries used in this test case. **Special JavaScript feature or syntax** The only special JavaScript feature used is the **template literals** (backticks) ``${i}`` in the preparation code. This allows for creating a string by embedding expressions inside it, making the code more readable and easier to maintain. **Other alternatives** If you need to filter an array of objects based on some condition, other alternatives could be: * Using a simple `if` statement with a loop or recursion. * Utilizing a third-party library like Lodash (e.g., `_filter()` method). * If you're using JavaScript's newer syntax and features, consider using the spread operator (`...`) in conjunction with an arrow function for more concise code. Keep in mind that these alternatives might have different performance characteristics and use cases compared to `reduce()` and `filter()`.
Related benchmarks:
filter-map vs reduce
filter-map vs reduce vs reduce with destructuring
filter-map vs reduce 2
filter + map vs reduce 123
filter-map vs reduce 100k
Comments
Confirm delete:
Do you really want to delete benchmark?