Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs foreach vs for..of (z2)
(version: 0)
Compare loop performance
Comparing performance of:
for vs foreach vs for..of
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = []; for (let i = 0; i < 10000000; i++) { array.push(i); } var object = {};
Tests:
for
for (let i = 0; i < array.length; i++) { object[array[i]] = true; }
foreach
array.forEach((value) => { object[value] = true; });
for..of
for (const value of array) { object[value] = true; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for
foreach
for..of
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):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares the performance of three different loop constructs in JavaScript: traditional `for` loops, `forEach` loops, and `for...of` loops. The test creates an array with 10 million elements and a nested object to store the results. **Loop Constructs Compared** 1. **Traditional `for` loop**: This is the classic loop construct that uses a counter variable (`i`) to iterate over the array elements. ```javascript for (let i = 0; i < array.length; i++) { object[array[i]] = true; } ``` Pros: Simple and widely supported, easy to read and write. Cons: Can be verbose and repetitive for large loops. 2. **`forEach` loop**: This is a more modern loop construct that uses the `Array.prototype.forEach()` method to iterate over the array elements. ```javascript array.forEach((value) => { object[value] = true; }); ``` Pros: Concise and expressive, eliminates the need for manual index management. Cons: May have performance overhead due to the overhead of calling a function on each element. 3. **`for...of` loop**: This is a newer loop construct that uses the `Array.prototype.forEach()` method but with a more concise syntax. ```javascript for (const value of array) { object[value] = true; } ``` Pros: Even more concise and expressive than the traditional `forEach` loop. Cons: May have similar performance overhead to the traditional `forEach` loop. **Library Used** In this benchmark, no specific library is used beyond the built-in `Array.prototype.forEach()` method. **Special JS Feature/Syntax** None of the loops in this benchmark use any special JavaScript features or syntax that would affect their performance. They are all standard, widely supported constructs. **Benchmark Results** The latest benchmark results show that: * The `for...of` loop is the fastest, with an average execution rate of 2.676398992538452 executions per second. * The traditional `for` loop is the slowest, with an average execution rate of 0.9049773812294006 executions per second. These results suggest that the newer `for...of` loop construct has a performance advantage over the traditional `for` loop and the `forEach` loop. **Alternative Loop Constructs** Other alternative loop constructs in JavaScript include: * `while` loops, which use a conditional statement to control iteration. * `do-while` loops, which iterate at least once before checking the condition. * Recursive functions, which use function calls to iterate over elements. However, these alternatives are often less efficient and more complex than the traditional loop constructs compared in this benchmark.
Related benchmarks:
Array fill foreach, vs for i loop
foreach vs for..of
foreach vs for...of
For loop vs <Array>.forEach() vs for...of loop
for vs foreach vs for..of vs for..of over entries vs for in
Comments
Confirm delete:
Do you really want to delete benchmark?