Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map + filter vs reduce
(version: 0)
compare map + filter vs reduce
Comparing performance of:
reduce vs filter+ map
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var customers = [ { active: true, name: "a", email: "b" }, { active: true, name: "a", email: "b" }, { active: true, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: true, name: "a", email: "b" }, { active: true, name: "a", email: "b" }, { active: true, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: true, name: "a", email: "b" }, { active: true, name: "a", email: "b" }, { active: true, name: "a", email: "b" }, { active: true, name: "a", email: "b" }, { active: true, name: "a", email: "b" }, { active: true, name: "a", email: "b" }, { active: true, name: "a", email: "b" }, { active: true, name: "a", email: "b" }, { active: true, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: false, name: "a", email: "b" } ];
Tests:
reduce
var emails = customers.reduce( (acc,c) => { if(c.active) { acc.push({ name: c.name, email: c.email } ); } return acc; }, []);
filter+ map
var emails = customers.filter( (c) => c.active) .map( (c) => ({ name: c.name, email: c.email }));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
reduce
filter+ map
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 its components. **Benchmark Definition** The benchmark is designed to compare the performance of two approaches: `map + filter` vs `reduce`. The input data is an array of customer objects, where each object has an `active` property indicating whether the customer is active or not. The goal is to extract the email addresses of only the active customers. **Benchmark Preparation Code** The preparation code generates a large array of 30 customer objects with varying `active` properties. This data will be used for both benchmark tests. **Html Preparation Code** There is no HTML preparation code provided, which means that this benchmark focuses solely on the JavaScript performance aspect. **Individual Test Cases** There are two test cases: 1. **"reduce"`**: This test case uses the `reduce()` method to iterate over the customers array and build an accumulator of email addresses. The callback function checks if each customer is active and pushes their email address to the accumulator if so. 2. **"filter+ map"`**: This test case uses the `filter()` method to filter out inactive customers, followed by the `map()` method to transform the remaining customers into a format with only their email addresses. **Comparison of Options** Now, let's analyze the pros and cons of each approach: * **`reduce()`**: + Pros: Can be more memory-efficient since it accumulates results in a single array. + Cons: May have higher overhead due to the initial accumulation of results, which can lead to slower performance for very large datasets. * **`filter + map`**: + Pros: Often faster and more efficient for large datasets, as it processes the data in two stages ( filtering and mapping). + Cons: Requires more memory since it creates an intermediate array with filtered customers. **Other Considerations** In this benchmark, we don't see any specialized JavaScript features or syntax being used. The focus is on the performance comparison between these two approaches. If you were to modify this benchmark to include special JS features or syntax, you might consider adding tests for: * `async/await` or `Promise.all()` for handling asynchronous operations * `let` and `const` declarations for variable scoping and hoisting * `class` declarations for object-oriented programming **Alternatives** If you're interested in exploring alternative approaches to this benchmark, here are some options: * **`forEach()`**: This method can be used as an alternative to both `reduce()` and `filter + map`. However, it might not be the most efficient choice for large datasets. * **`Array.prototype.forEach()` with a custom callback function**: Similar to `forEach()`, but allows for more control over the iteration process. * **`for...of` loop**: A modern JavaScript loop that can be used as an alternative to both `reduce()` and `filter + map`. However, it might not provide the same level of flexibility as the other options. I hope this explanation helps you understand the benchmark and its components!
Related benchmarks:
Filter and Map vs Reduce
flatMap vs reduce filtering performance
Reduce vs map with empty filter
Flat map + filter vs. Reduce
Reduce Push vs. flatMap vs 123
Comments
Confirm delete:
Do you really want to delete benchmark?