Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs foreach vs for-const vs for..of
(version: 0)
Compare loop performance
Comparing performance of:
for vs foreach vs for..of vs for-const
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = new Array(1000);
Tests:
for
let res=0 for (var i = 0; i < array.length; i++) { res += array[i] }
foreach
let res=0 array.forEach(function(i) { res += i });
for..of
let res=0 for (var i of array) { res += i }
for-const
let res=0 const leng = array.length for (var i = 0; i < leng; i++) { res += 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-const
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Browser/OS:
Chrome 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
468396.7 Ops/sec
foreach
1973586.6 Ops/sec
for..of
2434760.5 Ops/sec
for-const
479931.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided benchmark compares the performance of four different loop constructs in JavaScript: traditional `for` loops, `forEach` loops, `for...of` loops, and `for...const` loops. **Loop Constructs Compared** 1. **Traditional `for` loops**: These loops use a manual incrementing variable (`i`) to iterate over an array. 2. **`forEach` loops**: These loops use the built-in `Array.prototype.forEach()` method to iterate over an array, passing a callback function that will be executed for each element. 3. **`for...of` loops**: These loops use a syntax introduced in ECMAScript 2015 (ES6) to iterate over arrays and other iterable objects without requiring manual variable management. 4. **`for...const` loops**: These loops are similar to traditional `for` loops but use the `const` keyword to declare the loop variable, making it immutable. **Options Compared** The benchmark compares the performance of these four loop constructs: * Raw execution time (measured in executions per second) * Browser and device platform-specific results **Pros and Cons of Each Approach** 1. **Traditional `for` loops**: Pros: more control over iteration variables; cons: verbose, error-prone. 2. **`forEach` loops**: Pros: concise syntax; cons: less control over iteration variables, can be slower due to callback function overhead. 3. **`for...of` loops**: Pros: concise and readable syntax; cons: limited flexibility (e.g., no support for manual iteration). 4. **`for...const` loops**: Pros: similar benefits to traditional `for` loops with the added benefit of immutability; cons: slightly less control over iteration variables. **Library Used** None explicitly mentioned in the provided JSON, but it is likely that the benchmark uses a standard JavaScript environment (e.g., Node.js or a browser). **Special JS Features/Syntax** * **`for...of` loops**: Introduced in ECMAScript 2015 (ES6), providing a concise and readable syntax for iterating over arrays and other iterable objects. * **`for...const` loops**: Introduced in ECMAScript 2017, providing the benefits of traditional `for` loops with the added benefit of immutability. **Alternatives** If you're interested in exploring alternative loop constructs, consider: 1. **Closures**: Using a closure to encapsulate iteration logic can provide more control and flexibility than traditional loops. 2. **Generators**: Introducing generators as an alternative to traditional loops for iterating over arrays or other iterable objects. Keep in mind that the performance benefits of these alternatives may vary depending on your specific use case. **Conclusion** The benchmark provides a useful comparison of four different loop constructs, highlighting their strengths and weaknesses. By choosing the right loop construct for your use case, you can improve code readability, maintainability, and performance.
Related benchmarks:
foreach vs for..of
for vs foreach vs for..of vs for..of over entries (const)
foreach vs for...of
for-in vs foreach vs for-of
For loop vs <Array>.forEach() vs for...of loop
Comments
Confirm delete:
Do you really want to delete benchmark?