Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Loops for compared
(version: 0)
Compare loop performance
Comparing performance of:
for vs foreach vs for..of vs for..of over entries
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = Array.from({length: 100});
Tests:
for
for (let i = 0; i < array.length; i++) { const t = array[i]; }
foreach
array.forEach(function(v, i) { const t = v; });
for..of
for (var v of array) { const t = v; }
for..of over entries
for (var [i, v] of array.entries()) { const 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/121.0.0.0 Safari/537.36 OPR/107.0.0.0
Browser/OS:
Opera 107 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
122214.2 Ops/sec
foreach
14327177.0 Ops/sec
for..of
13869474.0 Ops/sec
for..of over entries
2701816.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll provide an in-depth explanation of the provided JSON benchmark data. **Benchmark Definition** The JSON defines a benchmark named "Loops for compared" that compares the performance of three different types of loops: traditional `for` loop, `forEach` loop, and `for..of` loop. The script preparation code is provided to create an array of 100 elements before running the benchmarks. **Options Compared** The three options compared in this benchmark are: 1. **Traditional `for` loop**: A classic loop that uses a counter variable (`i`) to iterate over the array elements. 2. **`forEach` loop**: A more modern and concise way to iterate over arrays using the `forEach` method. 3. **`for..of` loop**: A newer type of loop introduced in ECMAScript 2015, which allows iterating over iterable objects, including arrays. **Pros and Cons** * **Traditional `for` loop**: + Pros: Easy to understand, control over iteration variables, and can be more efficient for certain use cases. + Cons: Can lead to tedious code when working with large datasets or complex logic. * **`forEach` loop**: + Pros: Concise, easy to read, and less prone to errors compared to traditional `for` loops. It's also generally faster than traditional `for` loops. + Cons: Less control over iteration variables, which can be a limitation for certain use cases. * **`for..of` loop**: + Pros: Allows iterating over arrays and other iterable objects, eliminating the need to keep track of indices or using `length`. It's also more concise than traditional `for` loops. + Cons: Less control over iteration variables, which can be a limitation for certain use cases. The syntax can be unfamiliar to developers who aren't familiar with it. **Library and Purpose** None of the options listed rely on any external libraries. They are built-in JavaScript features designed to improve code readability and performance. **Special JS Features or Syntax** The `for..of` loop uses a new type of iteration syntax introduced in ECMAScript 2015, which allows iterating over arrays and other iterable objects using a more concise syntax than traditional `for` loops. This feature is not present in older versions of JavaScript. **Other Alternatives** For other types of loops or performance comparisons, some common alternatives include: * `while` loops * Array methods like `map()`, `filter()`, or `reduce()` * Other libraries or frameworks that provide optimized loop implementations Keep in mind that the choice of loop type ultimately depends on the specific requirements and constraints of your project.
Related benchmarks:
for vs foreach vs for of
for vs foreach vs for..of vs for..of over entries
for vs foreach vs for..of vs for..of over entries vs for in
for vs foreach vs for..of vs for..of over entries vs for in vs for cache vs for reverse
Comments
Confirm delete:
Do you really want to delete benchmark?