Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs foreach vs for..of vs for..of over entries (const)
(version: 0)
Compare loop performance
Comparing performance of:
for vs foreach vs for..of vs for..of over entries
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = Array.from({length: 100}); var t;
Tests:
for
for (let i = 0; i < array.length; i++) { t = array[i]; }
foreach
array.forEach(function(v, i) { t = v; });
for..of
for (const v of array) { t = v; }
for..of over entries
for (const [i, v] of array.entries()) { t = v; }
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..of over entries
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
72006.3 Ops/sec
foreach
171216.4 Ops/sec
for..of
171911.9 Ops/sec
for..of over entries
159427.1 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. **What is being tested?** The main goal of this benchmark is to compare the performance of four different loop constructs in JavaScript: 1. Traditional `for` loop 2. `forEach` method (a built-in array method) 3. `for...of` loop with a variable declaration 4. `for...of` loop with an iterator object and `.entries()` method These loops will be used to iterate over the same array, accessing its elements. **Options compared** The four options being compared are: 1. Traditional `for` loop: A manual loop construct that uses a counter variable (`i`) to access array elements. 2. `forEach` method: A built-in method that executes a function once for each element in an array. 3. `for...of` loop with variable declaration: A newer loop construct that iterates over the elements of an array using a single statement. 4. `for...of` loop with iterator object and `.entries()` method: Another variant of the `for...of` loop, which uses an iterator object to access the array's elements. **Pros and cons** Here are some general pros and cons for each option: 1. Traditional `for` loop: * Pros: Fully controlled iteration, flexible conditional statements. * Cons: More verbose, error-prone (e.g., off-by-one errors). 2. `forEach` method: * Pros: Concise, easy to use, built-in. * Cons: Less control over iteration, can be slower due to function call overhead. 3. `for...of` loop with variable declaration: * Pros: Modern, concise, and efficient. * Cons: Limited control over iteration (no conditional statements). 4. `for...of` loop with iterator object and `.entries()` method: * Pros: Offers more flexibility than the simple `for...of` loop, still concise. * Cons: Requires additional setup (iterator object creation). **Library usage** In this benchmark, none of the libraries are explicitly mentioned. However, it's worth noting that some modern browsers may use internal implementations of these loop constructs that might have performance differences. **Special JavaScript features or syntax** None of the test cases explicitly use special JavaScript features like async/await, arrow functions, or classes. **Other alternatives** For similar benchmarking purposes, other options could be considered: 1. `while` loop 2. `do...while` loop 3. Array destructuring (e.g., `const [i, v] = array`) 4. Other modern loop constructs like `let i = 0; while(i < array.length) { ... }` These alternatives might be worth exploring in a separate benchmark to compare their performance with the traditional loop constructs. Overall, this benchmark aims to evaluate the performance of different loop constructs in JavaScript, providing insights into the efficiency and practicality of each option for common use cases.
Related benchmarks:
foreach vs for..of
Get index with forEach vs for...of over entries
foreach vs for...of
For loop vs <Array>.forEach() vs for...of loop
Comments
Confirm delete:
Do you really want to delete benchmark?