Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs for in with index 2
(version: 0)
Comparing performance of:
for vs for..of with index
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; for (let i = 0; i < 100000; i++) { arr[i] = i*2; }
Tests:
for
const length = arr.length; for (let i = 0; i < length; i++) { arr[i] = arr[i]*2; }
for..of with index
let index = -1; for (let i of arr) { index++; i = i*2 };
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for
for..of with index
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
gemma2:9b
, generated one year ago):
This benchmark compares two different approaches to iterating over an array in JavaScript: **Option 1: `for` loop with index** * **Benchmark Definition:** `const length = arr.length; for (let i = 0; i < length; i++) { arr[i] = arr[i] * 2; }` * This classic approach uses a traditional `for` loop and explicitly tracks the index (`i`) of each element in the array. **Option 2: `for...of` loop with index** * **Benchmark Definition:** `let index = -1; for (let i of arr) { index++; i = i * 2; }` * This method utilizes the more modern `for...of` loop, which iterates directly over each element in the array without requiring an explicit index. We still add a variable `index` to mimic the traditional approach's behavior. **Pros and Cons:** * **`for` Loop:** * **Pros:** Well-established, widely understood syntax. Can be more performant for operations that heavily rely on indexed access. * **Cons:** More verbose, requires explicit index management. * **`for...of` Loop:** * **Pros:** Concise and cleaner syntax. Easier to read and understand for iterating over elements. * **Cons:** Can be slightly less performant than `for` loops in scenarios where indexed access is frequent. **Other Considerations:** * **Array Length:** The benchmark uses a relatively large array (100,000 elements). This is to make any performance differences more noticeable. * **Task:** Both approaches perform the same operation: doubling each element's value. This is a simple operation that doesn't heavily rely on specific array features. **Alternatives:** * **`Array.forEach()`:** A higher-order function that iterates over an array and executes a provided callback function for each element. Let me know if you have any more questions or want to explore other benchmark examples!
Related benchmarks:
for vs for in with index
testtmptmptmp
while vs for vs forEach vs for of - 100000000 - calc exponent multiple
Uint8Array vs Uint8ClampedArray - more numbers
Comments
Confirm delete:
Do you really want to delete benchmark?