Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
forEach vs reduce vs map vs filter vs for tiny
(version: 0)
same as before but only 100 items
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 < 100; 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 (var j = 0; j < arr.length; j++) { sumFor += 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 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 136 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
forEach
32730.2 Ops/sec
reduce
33950.5 Ops/sec
map
29215.0 Ops/sec
filter
28620.4 Ops/sec
for
45672.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases to understand what is being tested. **Benchmark Definition** The benchmark measures the performance of different JavaScript iteration methods: `forEach`, `reduce`, `map`, `filter`, and a traditional `for` loop. The script preparation code creates an array of 100 elements, each containing a specific calculation using the `someFn` function. **Library used** In the provided test cases, no libraries are explicitly mentioned. However, it's likely that the JavaScript engine being tested is a browser engine (e.g., Blink for Chrome) and doesn't require any additional libraries. **Iteration methods compared** The benchmark tests the performance of five different iteration methods: 1. `forEach`: Iterates over an array using a callback function. 2. `reduce`: Accumulates values in an array by applying a callback function to each element. 3. `map`: Creates a new array with transformed elements using a callback function. 4. `filter`: Creates a new array with filtered elements using a callback function. 5. Traditional `for` loop: Iterates over an array manually using a `for` loop. **Pros and cons of each approach** Here's a brief overview of the pros and cons of each iteration method: 1. `forEach`: * Pros: Easy to use, doesn't require explicit indexing. * Cons: Can be slower than other methods due to the overhead of callback functions. 2. `reduce`: * Pros: Efficient for accumulating values, can handle initial value and iterate-reduce logic in one pass. * Cons: Requires understanding of accumulator and callback function usage. 3. `map`: * Pros: Creates a new array with transformed elements, can be used for parallel processing. * Cons: Can create unnecessary intermediate arrays, can be slower than other methods if not optimized. 4. Traditional `for` loop: * Pros: Direct control over iteration, no overhead from callback functions. * Cons: Requires explicit indexing and manual iteration management. **Other considerations** When choosing an iteration method, consider the following factors: * Data size and structure * Performance requirements * Code readability and maintainability * Parallel processing needs **Benchmark results** The provided benchmark results show the number of executions per second for each iteration method. The `reduce` method seems to be the fastest, followed by `forEach`, `map`, `filter`, and traditional `for` loop. Keep in mind that these results may not reflect real-world performance differences due to various factors like hardware, browser optimizations, and code complexity.
Related benchmarks:
forEach vs reduce vs map vs filter vs for
forEach vs reduce vs map vs filter vs for (slightly optimized for, fixed fn)
forEach vs reduce vs map vs filter vs for vs for..of big list
forEach vs reduce vs map vs filter vs for (slightly optimized for (fixed))
Comments
Confirm delete:
Do you really want to delete benchmark?