Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs forEach vs for-of vs for-reverse (short array summing)
(version: 0)
for vs forEach vs for-of vs for-reverse
Comparing performance of:
for vs forEach vs for-of vs for-reverse
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = Array.from({length: 10}, (_, index) => index);
Tests:
for
var sum = 0 for (var i = 0; i < array.length; i++) { sum += array[i]; }
forEach
var sum = 0 array.forEach(function(item) { sum += item; });
for-of
var sum = 0 for (var item of array) { sum += item; }
for-reverse
var sum = 0 for (var i = array.length; i--;) { sum += array[i]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
for
forEach
for-of
for-reverse
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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36 Edg/132.0.0.0
Browser/OS:
Chrome 132 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
42409064.0 Ops/sec
forEach
60335220.0 Ops/sec
for-of
81751888.0 Ops/sec
for-reverse
52530820.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided JSON represents a JavaScript microbenchmark test case, specifically comparing the performance of four different loop constructs: traditional `for`, `forEach`, `for-of`, and `for-reverse`. Let's dive into each option: **Traditional `for` Loop** * Description: This is the most common type of loop in JavaScript. It uses an explicit counter variable (`i`) to iterate over the array. * Pros: + Simple and straightforward + Easy to understand and maintain * Cons: + Can be verbose and cluttered with unnecessary variables + May not be as efficient as other options **`forEach` Loop** * Description: The `forEach` method is a part of the Array prototype, which allows you to execute a callback function for each element in an array. * Pros: + Concise and easy to read + Reduces boilerplate code * Cons: + May not be as performant as traditional loops due to the overhead of the `forEach` method + Can be slower on older browsers **`for-of` Loop** * Description: The `for-of` loop is a new iteration of the traditional `for` loop, introduced in ECMAScript 2015 (ES6). It allows you to iterate over arrays without declaring an explicit counter variable. * Pros: + More concise and readable than traditional `for` loops + Reduces boilerplate code + Faster execution compared to traditional `for` loops on modern browsers * Cons: + May require additional browser support (although widely supported in recent versions) **`for-reverse` Loop** * Description: This loop is similar to the traditional `for` loop but iterates over the array elements in reverse order. * Pros: + Useful for operations that require iterating over an array in reverse * Cons: + May be less performant than other options due to the need to iterate in reverse In terms of special JavaScript features, there are no explicit mentions of features like `let`, `const`, or arrow functions, which may have been used in some of the benchmark definitions. The test case uses the built-in `Array.prototype.forEach` method, which is a part of the Array prototype. This method is designed to iterate over array elements and execute a callback function for each element. Some considerations when choosing between these loop constructs: * Performance: If performance is critical, the traditional `for` loop might be faster due to its lack of overhead. * Code readability: The `forEach` loop is generally more concise and readable than the traditional `for` loop. * Modern browser support: The `for-of` loop requires modern browsers (though widely supported in recent versions). Other alternatives for these loop constructs: * Traditional `while` loops or recursive functions * Other array methods like `map`, `filter`, or `reduce` * Functional programming approaches using `Array.prototype.reduce()` or other higher-order functions Keep in mind that the performance differences between these loop constructs may be relatively small, and the choice ultimately depends on your specific use case, personal preference, and code readability goals.
Related benchmarks:
for vs forEach vs for-of vs for-reverse (calculate sum)
for vs forEach vs for-of vs for-reverse vs reduce (calculate sum)
for vs forEach vs for-of vs for-reverse (short array summing 2)
.forEach vs for const of
Comments
Confirm delete:
Do you really want to delete benchmark?