Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array looping entries
(version: 0)
Finding out the performance hit of array entries
Comparing performance of:
for of loop vs Simple for loop (reverse)
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; for( var cpt = 0; cpt < 1000; cpt++ ) { arr.push( Math.random() ); }
Tests:
for of loop
const arr2 = [] for (const entry of arr) { arr2.push(entry) }
Simple for loop (reverse)
const arr2 = [] for (const [i, entry] of arr.entries()) { arr2.push(entry) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for of loop
Simple for loop (reverse)
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 provided JSON and explain what's being tested, compared, and analyzed in the benchmark. **What is being tested?** The benchmark tests two different approaches to iterate over an array: 1. **Traditional `for` loop**: The first test case uses a traditional `for` loop with a counter variable (`cpt`) to iterate over the array entries. 2. **`for...of` loop**: The second test case uses the new `for...of` loop syntax, which iterates over an array's entries using an iterator object. **What options are compared?** The benchmark compares the performance of these two approaches: * Traditional `for` loop with a counter variable * `for...of` loop with an iterator object **Pros and Cons:** 1. **Traditional `for` loop**: * Pros: Widely supported, easy to understand, and maintain. * Cons: Can be less efficient due to the need for a separate counter variable. 2. **`for...of` loop**: * Pros: More concise, eliminates the need for a separate counter variable, and can improve performance in some cases. * Cons: May have slightly higher overhead due to the creation of an iterator object. **Other considerations:** * The benchmark also tests the reverse iteration ( `arr.entries()` ) which is not included in the provided JSON. It's worth noting that this approach can be even more efficient than traditional for loops, especially when dealing with large arrays. * The test cases are designed to exercise the array iteration algorithms and gather data on their performance characteristics. **Library/Functionality usage:** There is no explicit library or functionality used in these benchmark tests. However, note that some JavaScript engines (like V8) provide additional features like array iterators, which can be leveraged for more efficient iteration. **Special JS feature/syntax:** The `for...of` loop syntax is a relatively new feature introduced in ECMAScript 2015 (ES6). It allows iterating over arrays and other iterable objects using a concise syntax.
Related benchmarks:
Fill array with random integers
Preinitialized array size vs Push operations to an empty one.
Array .push() vs .unshift() with random numbers
Iterator vs Index for loop (cached array length fixed)
Comments
Confirm delete:
Do you really want to delete benchmark?