Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for(const x of arr) vs. for(let i = 0; i < arr.length; i++)
(version: 0)
Comparing performance of:
for(const x of arr) vs for(let i = 0; i < arr.length; ++i)
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
for(const x of arr)
const arr = [] for (let i = 0; i < 10000; i++) { arr.push(i) } for(const hi of arr) { }
for(let i = 0; i < arr.length; ++i)
const arr = [] for (let i = 0; i < 10000; i++) { arr.push(i) } for(let i = 0; i < arr.length; ++i) { const self = arr[i] }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for(const x of arr)
for(let i = 0; i < arr.length; ++i)
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 dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Definition** The provided JSON represents a benchmark definition for two test cases: 1. `for(const x of arr)` 2. `for(let i = 0; i < arr.length; ++i)` These are both `for` loops that iterate over an array (`arr`) with a different syntax. **Options Compared** In this benchmark, the following options are compared: * **Syntax**: Two different `for` loop syntaxes are used: + `const x of arr`: This is called the "iterable for" or "array iteration" syntax, introduced in ECMAScript 2015 (ES6). It allows iterating over arrays and other iterable objects using a `for...of` loop. + `let i = 0; i < arr.length; ++i`: This is the traditional `for` loop syntax, where the variable `i` is initialized before the loop and incremented manually inside the loop body. * **Performance**: The benchmark measures the execution speed of each loop using a timer. **Pros and Cons** Here are some pros and cons for each approach: * **Iterable for (const x of arr)** + Pros: - More concise and readable syntax - Less boilerplate code required - Can be faster due to reduced overhead from manual variable management + Cons: - May have slightly worse performance in some cases due to additional overhead from creating an iterator object * **Traditional for (let i = 0; i < arr.length; ++i)** + Pros: - Well-established and widely supported syntax - May be faster in some cases due to reduced overhead from iterable creation + Cons: - More verbose and less readable syntax - Requires manual variable management, which can lead to errors **Library Used** In this benchmark, no external libraries are used. **Special JS Features or Syntax** The `for...of` loop (const x of arr) is a feature introduced in ECMAScript 2015 (ES6). It allows iterating over arrays and other iterable objects using a single syntax. This feature was added to provide a more concise and readable way to iterate over collections. **Other Alternatives** If you're interested in exploring alternative loop constructs, here are some examples: * `for...in` loop: Iterates over the properties of an object. * `forEach` method: A part of the DOM API, allows iterating over elements in a document or subdocument. * Array.prototype.map(): Applies a transformation function to each element in an array and returns a new array. Keep in mind that these alternatives may have different use cases and performance characteristics compared to traditional `for` loops.
Related benchmarks:
for vs for in with index 2
Reading array length inside vs outside for loop
push vs set length and index vs index vs typedArray to array vs typedArray
For Loop Leng Inside and Outside
Comments
Confirm delete:
Do you really want to delete benchmark?