Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs forEach vs reduce
(version: 0)
Benchmark to test performance of for vs forEach vs reduce when N is under 50
Comparing performance of:
Test For vs Test ForEach vs Test Reduce
Created:
7 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; for(var i = 0; i < 20; i++) { arr.push(i); } function someFn(i) { return (i * 3 * 8 / 1200 * 0.002 / 40 * 0.2); } var sumFor = 0; var sumForEach = 0; var sumReduce = 0;
Tests:
Test For
for(var i = 0; i < arr.length; i++) { sumFor += someFn(arr[i]); }
Test ForEach
arr.forEach(value => sumForEach += someFn(value))
Test Reduce
sumReduce = arr.reduce((lastValue, item) => { return sumReduce += someFn(item); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Test For
Test ForEach
Test Reduce
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):
**What is tested on the provided JSON?** The benchmark tests the performance of three different approaches for iterating over an array and performing calculations: 1. **For loop**: A traditional `for` loop uses an index variable to iterate over the elements of the array. 2. **`forEach` method**: The `forEach` method is a built-in JavaScript function that allows you to execute a callback function once for each element in an array. 3. **`reduce` method**: The `reduce` method is another built-in JavaScript function that applies a function to each element of the array and reduces it to a single value. The benchmark measures how many executions per second each approach can perform on a 20-element array, with a specific mathematical function (`someFn`) applied to each element. **Options compared** The three options are compared in terms of their performance, which is measured by the number of executions per second (ExecutionsPerSecond). **Pros and Cons of each approach** * **For loop**: Pros: + Easy to understand and implement + No additional memory allocated for the iteration process Cons: + Can be slower than other approaches due to the overhead of incrementing the index variable * **`forEach` method**: Pros: + Concise and readable code + Does not require manual management of the index variable Cons: + May have slower performance compared to `for` loops, especially for large arrays * **`reduce` method**: Pros: + Can be more efficient than `forEach` due to the use of a single array update operation + Can simplify complex calculations by reducing them to a single value Cons: + May require additional memory allocation and can be less readable for some developers **Library** In this benchmark, no libraries are explicitly mentioned. However, it is likely that JavaScript's built-in functions (e.g., `Array.prototype.forEach` and `Array.prototype.reduce`) are being used. **Special JS feature or syntax** None of the test cases use any special JavaScript features or syntax beyond what is considered standard in modern JavaScript programming. **Other alternatives** There are other approaches to iterate over arrays and perform calculations, such as: * **Using `map`**: Instead of using a loop or `forEach`, you can use the `map` method to create a new array with transformed values. * **Using iterators or generators**: For more complex scenarios, you might consider using iterators or generators to traverse the array, especially when dealing with large datasets. These alternatives are not part of this specific benchmark and would require additional code changes.
Related benchmarks:
sum calculation: forEach vs for vs forof vs reduce 2
Sum speed test
for of vs Array.reduce vs Array.forEach vs for i for summing and array of integers
Array.prototype.reduce() vs for loop sum
Comments
Confirm delete:
Do you really want to delete benchmark?