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))
(version: 0)
Comparing performance of:
forEach vs reduce vs map vs filter vs for
Created:
4 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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
forEach
1230.3 Ops/sec
reduce
1251.9 Ops/sec
map
1264.1 Ops/sec
filter
1207.8 Ops/sec
for
1347.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the provided JavaScript microbenchmark, MeasureThat.net. **Benchmark Definition** The benchmark tests the performance of four different approaches to iterate over an array and perform a calculation on each element: `forEach`, `reduce`, `map`, and `filter`. The calculation involves multiplying the input number by 3, then dividing it by 1200, and finally multiplying the result by 0.002 and dividing it by 40. This is followed by multiplying the final result by 0.2. **Options Compared** The benchmark compares the performance of these four approaches: 1. **`forEach`**: Iterates over the array using a traditional `for` loop and calls the callback function on each element. 2. **`reduce`**: Uses the `reduce()` method to accumulate the results of applying a function to each element in the array. 3. **`map`**: Applies a transformation function to each element in the array, returning an array with the new values. 4. **`filter`**: Creates a new array containing only the elements that satisfy the provided filtering condition. **Pros and Cons** * **`forEach`**: Pros: + Easy to read and understand. + Does not require creating a new data structure (in contrast to `map` or `reduce`). * Cons: + Requires an explicit loop, which can be less efficient than the optimized loop used in other approaches. * **`reduce`**: Pros: + Can accumulate results efficiently using a single pass through the array. + Reduces memory allocation by reusing the accumulator variable. * Cons: + Requires creating an initial value for the accumulator, which can lead to unnecessary allocations if not properly initialized. * **`map`**: Pros: + Returns a new array with transformed values, allowing for easier testing and debugging. + Can be more memory-efficient than `forEach` if the entire array is used. * Cons: + Requires creating a new data structure (the resulting array), which can lead to increased memory allocation. * **`filter`**: Pros: + Efficiently eliminates unnecessary elements from the array without modifying it. + Can be more concise and readable than `forEach`. * Cons: + Returns a new array, which may not be desirable for performance-critical applications. **Library** The benchmark uses an internal library to execute the test cases. MeasureThat.net does not provide explicit documentation on this library, but it is likely a custom implementation optimized for JavaScript performance testing. **Special JS Features/Syntax** None of the provided test cases utilize any special JavaScript features or syntax. They are written in standard JavaScript and should be compatible with most modern browsers. **Alternatives** If you're interested in exploring alternative approaches to this benchmark, consider the following options: 1. **`every()` and `some()`**: Instead of `filter`, you could use `every()` and `some()` to check if all or at least one element in the array meets a condition. 2. **`Array.prototype.every()` and `Array.prototype.some()`**: If available, these methods provide more concise ways to test array elements using higher-order functions. 3. **Other Iteration Methods**: Depending on your specific use case, you might prefer alternative iteration methods like `while` loops or `for...of` loops. Keep in mind that the performance differences between these approaches can be small, and the results may vary depending on your specific JavaScript engine implementation and environment.
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 fn)
Comments
Confirm delete:
Do you really want to delete benchmark?