Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
forEach vs reduce vs map vs filter vs for (slightly optimized for, fixed fn)
(version: 0)
Comparing performance of:
forEach vs reduce vs map vs filter vs for
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; for (var i = 0; i < 12345; i++) { arr[i] = i; } function someFn(i) { return (i * 3 * 8 / 1200 * 0.002 / 40 * 0.2); } var sumForEach = 0, sumReduce = 0, sumMap = 0, sumFilter = 0, sumFor = 0;
Tests:
forEach
arr.forEach(item => sumForEach += someFn(item));
reduce
sumReduce = arr.reduce((lastValue, item) => { return sumReduce += someFn(item); });
map
arr.map(item => (sumMap += someFn(item)));
filter
arr.filter(item => (sumFilter += someFn(item)));
for
for (let j = 0, len = arr.length; j < len; j++) { sumFor += someFn(arr[j]); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
forEach
reduce
map
filter
for
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:135.0) Gecko/20100101 Firefox/135.0
Browser/OS:
Firefox 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
forEach
21385.2 Ops/sec
reduce
21184.1 Ops/sec
map
15768.8 Ops/sec
filter
14013.0 Ops/sec
for
47011.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the provided benchmark and explore what's being tested. **Overview** The benchmark compares the performance of four JavaScript methods: `forEach`, `reduce`, `map`, and `filter`. Each method is used to iterate over an array (`arr`) and perform a specific operation on its elements. The benchmark also includes a simple function `someFn` that calculates a value based on the input. **Tested Options** The benchmark tests the following options: 1. **forEach**: Iterates over the array using `forEach`, adding the result of `someFn(item)` to `sumForEach`. 2. **reduce**: Iterates over the array using `reduce`, accumulating the results of `someFn(item)` in `sumReduce`. 3. **map**: Iterates over the array using `map`, returning an array with the results of `someFn(item)`, and summing them up. 4. **filter**: Iterates over the array using `filter`, selecting elements for which `someFn(item)` is truthy, and summing them up in `sumFilter`. 5. **for (loop)**: Uses a traditional `for` loop to iterate over the array, calling `someFn(arr[j])` and adding the result to `sumFor`. **Pros and Cons** Here's a brief summary of the pros and cons for each approach: 1. **forEach**: * Pros: Easy to read and maintain, efficient use of built-in method. * Cons: May not be as cache-friendly as other methods (e.g., `reduce`). 2. **reduce**: * Pros: Efficient use of accumulator pattern, can take advantage of caching. * Cons: More complex to understand, may require additional setup. 3. **map**: * Pros: Can be used for transformations, easy to understand. * Cons: Creates a new array, which can lead to increased memory usage. 4. **filter**: * Pros: Efficient use of Boolean operations, easy to understand. * Cons: May not be as cache-friendly as other methods (e.g., `reduce`). 5. **for (loop)**: * Pros: Simple and straightforward, no built-in method overhead. * Cons: Not optimized for performance, more prone to errors. **Library and Special JS Features** In this benchmark, the following library is used: * None explicitly mentioned, but it's likely that the `Array` prototype methods (`forEach`, `reduce`, `map`, and `filter`) are being utilized, which are built-in to JavaScript. There are no special JavaScript features or syntaxes mentioned in this benchmark. **Alternatives** Other alternatives for iterating over arrays in JavaScript include: * `forEach`: Built-in method, easy to use. * `for...in`/`for...of`: Traditional loop constructs, can be used with `Array.prototype.forEach`. * `while`: Loop construct, can be used without `Array.prototype.forEach`. * `Lambda expressions`: Can be used in place of traditional functions for iteration. * Third-party libraries or frameworks (e.g., Lodash) that provide optimized array methods. Keep in mind that the choice of iteration method depends on the specific use case and performance requirements.
Related benchmarks:
forEach vs reduce vs map vs filter vs for
forEach vs reduce vs map vs filter vs for tiny
forEach vs reduce vs map vs filter vs for v2292U9I2JIR2J0IEJ02JE0IJ20EJ
forEach vs reduce vs map vs filter vs for (slightly optimized for (fixed))
Comments
Confirm delete:
Do you really want to delete benchmark?