Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Iterator vs Index for loop (cached array length fixed)
(version: 1)
Comparing performance of:
Iterator for loop vs Index for loop
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = []; for(let i = 0; i < 10000; i++) { arr.push(Math.random()); } var arrLength = arr.length;
Tests:
Iterator for loop
let sum = 0; for(const num of arr) { sum += num; }
Index for loop
let sum = 0; for(let i = 0; i < arrLength; i++) { sum += arr[i]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Iterator for loop
Index for loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0
Browser/OS:
Chrome 133 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Iterator for loop
19373.6 Ops/sec
Index for loop
22203.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the provided benchmark and explain what is being tested. The test compares two approaches to iterate over an array: using an iterator (forEach) versus using index-based indexing ( traditional for loops). The array length is fixed at 10,000 elements, which is already cached before running the benchmarks. **Pros and Cons of each approach:** 1. **Iterator (forEach) Approach:** * Pros: + More concise and readable code + Less chance of off-by-one errors or array index out-of-bounds issues + Better suited for arrays that need to be iterated over multiple times without re-creating the iterator * Cons: + Can lead to increased garbage collection pauses if not done carefully (using `const` and avoiding unnecessary variable assignments) + Might have performance overhead due to the iterator's internal mechanics 2. **Index-Based Indexing Approach:** * Pros: + Direct access to array elements, which can be faster for simple iteration scenarios + Less chance of garbage collection pauses since no new objects are created during iteration * Cons: + Can lead to more complex and error-prone code due to the need to manage array indices manually + More likely to have off-by-one errors or array index out-of-bounds issues **Library:** There is no explicitly mentioned library in this benchmark, but `forEach` (also known as "for...of" loop) is a built-in JavaScript feature that leverages the V8 engine's iterator implementation. The test is comparing the performance of using the built-in `forEach` method versus traditional for loops. **Special JS Features:** This benchmark does not use any special JavaScript features or syntax beyond the standard language and the provided libraries (V8 engine). **Alternative Benchmarks:** Other alternatives to compare in this type of benchmark could include: * Using other iteration methods, such as `map()` or `reduce()` * Iterating over arrays with different types, like objects or sets * Adding noise to the array elements (e.g., adding random values) to simulate real-world scenarios where performance may degrade due to extra overhead Keep in mind that the choice of alternatives depends on the specific use case and requirements of the benchmark.
Related benchmarks:
Preinitialized array size vs Push operations to an empty one.
Array .push() vs .unshift() with random numbers
Iterator vs Index for loop
Iterator vs Index for loop (global sum)
Comments
Confirm delete:
Do you really want to delete benchmark?