Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for of vs Array.reduce vs Array.forEach vs for i for summing and array of integers
(version: 0)
Test which looping method has the best performance regarding getting the total sum of an array of integers in Javascript
Comparing performance of:
for of vs forEach vs reduce vs for i
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; for(var i=0; i < 10000; i++) { arr.push(Math.floor(Math.random() * Math.floor(10000))); }
Tests:
for of
var sum = 0; for (var e of arr) { sum += e; }
forEach
var sum = 0; arr.forEach(e => {sum += e;})
reduce
var sum = arr.reduce((acm, current) => acm + current);
for i
var sum = 0; for (var i = 0; i < arr.length; i++) { sum += arr[i]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
for of
forEach
reduce
for i
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/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for of
158769.2 Ops/sec
forEach
60427.2 Ops/sec
reduce
91042.8 Ops/sec
for i
1699.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the JavaScript microbenchmark on MeasureThat.net. **Benchmark Overview** The benchmark measures the performance of four looping methods in JavaScript: 1. `for...of` 2. `Array.forEach` 3. `Array.reduce` 4. `for` loop with a traditional index variable (`i`) Each looping method is used to calculate the sum of an array of 10,000 random integers. **Options Compared** The benchmark compares the performance of these four looping methods: * **Performance**: The execution speed of each looping method. * **Cons**: Each looping method has its own trade-offs: + `for...of` loops are concise and easy to read, but may not be as efficient as traditional loops. + `Array.forEach` loops iterate over the array's elements, but require an explicit callback function. + `Array.reduce` is a functional programming approach that reduces the array to a single value, which can be efficient for certain use cases. + Traditional `for` loops are straightforward and often used, but may not be as concise or expressive as other looping methods. **Pros and Cons** Here's a brief summary of each looping method: * **For...of**: Pros - concise, easy to read; Cons - potential performance overhead due to the iterator object. * **Array.forEach**: Pros - convenient and expressive; Cons - requires an explicit callback function, which can lead to code duplication. * **Array.reduce**: Pros - functional programming approach, efficient for summing or reducing arrays; Cons - may be less intuitive for those unfamiliar with functional programming concepts. * **For** loop: Pros - straightforward and traditional; Cons - potentially less concise and readable. **Library and Special JS Features** None of the looping methods rely on external libraries. However, some features are used: * `Math.floor` is used to generate random numbers within a specific range. * The `Array.prototype.forEach`, `Array.prototype.reduce`, and `Array.prototype.push` methods are used by each looping method. **Test Case Explanation** Each test case uses the same script preparation code to initialize an array of 10,000 random integers. Then, each looping method is tested in isolation: 1. `for...of`: Iterates over the array using the `for...of` loop construct. 2. `Array.forEach`: Iterates over the array using the `forEach` method with a callback function. 3. `Array.reduce`: Reduces the array to a single value using the `reduce` method with an accumulator function. 4. Traditional `for` loop: Iterates over the array using a traditional `for` loop construct. **Other Alternatives** If you're interested in exploring other looping methods, consider: * `Array.prototype.map` * Arrow functions (`=>`) * `Set` or `Map` data structures for more complex operations Feel free to experiment with these alternatives and compare their performance on MeasureThat.net!
Related benchmarks:
Array.reduce vs for loops vs Array.forEach
Iterator vs Index for loop (cached array length fixed)
Iterator vs Index for loop (global sum)
Array.prototype.reduce() vs for loop sum
Comments
Confirm delete:
Do you really want to delete benchmark?