Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
.filter().map() vs .reduce test
(version: 0)
Comparing performance of:
.map().filter() vs .reduce()
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = []; for (var i = 0; i < 10000; i++) { array[i] = i; } var firstTestResults = []; var secondTestResults = [];
Tests:
.map().filter()
firstTestResults = array .filter(value => { return (value + 10) % 2 === 0; }) .map(value => { return value + 10; });
.reduce()
secondTestResults = array.reduce((acc, value) => { value = value + 10; if (value % 2 === 0) { acc.push(value); } return acc; }, []);
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:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
.map().filter()
20116.7 Ops/sec
.reduce()
31043.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares the performance of two approaches to process an array of numbers: 1. `.map()` followed by `.filter()` 2. `.reduce()` Both tests are executed on a large array of 10,000 elements, where each element is initialized with its index (`i`). **Options Being Compared** Two options are being compared: * `Array.prototype.map()`: This method creates a new array with the results of applying a provided function to every element in the original array. * `Array.prototype.reduce()`: This method reduces an array to a single value, executing a callback function on each element in the array. **Pros and Cons of Each Approach** * `.map()`: + Pros: Simple, easy to understand, and intuitive. It creates a new array with the desired output, making it easier to reason about the code. + Cons: Can be slower for large arrays since it creates a new array with all the results, which can lead to memory issues. * `.reduce()`: + Pros: More flexible and efficient than `map()` when dealing with large arrays. It avoids creating a new array with intermediate results. + Cons: Less intuitive and more complex to understand compared to `map()`. **Library Usage** There is no explicit library usage in the provided benchmark code, but it's worth noting that both `.map()` and `.reduce()` are built-in methods of the Array prototype in JavaScript. **Special JS Feature/Syntax** None of the benchmarks explicitly use any special JavaScript features or syntax. However, if you're interested in exploring other options, there are some alternative approaches, such as using `forEach()`, `for...of` loops, or even using a custom loop to iterate through the array. **Alternatives** If you want to explore alternative approaches, here are a few options: * Using `forEach()`: ```javascript array.forEach(function(value) { // process value }); ``` This approach uses an inline function and does not create any new arrays or objects. * Using a `for...of` loop: ```javascript for (const value of array) { // process value } ``` This approach is similar to the `forEach()` method but uses a more modern syntax. **Other Considerations** When choosing between these approaches, consider the following: * **Performance**: If you need to process large arrays and memory efficiency is crucial, `.reduce()` might be a better choice. However, if simplicity and ease of understanding are more important, `.map()` followed by `.filter()` might be a better fit. * **Code readability**: If code readability is essential, consider using `map()` or `forEach()`, as they provide clear and concise ways to process arrays. I hope this explanation helps you understand the provided benchmark and provides a solid foundation for exploring JavaScript performance optimizations!
Related benchmarks:
Map from .reduce vs Map from .map
filter + map vs reduce 123
flatMap vs reduce vs filter.map
Filter and Map vs Reduce
Reduce Push vs. flatMap vs 123
Comments
Confirm delete:
Do you really want to delete benchmark?