Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array iteration Large - continue
(version: 2)
Comparing performance of:
for element of LARGE vs for const element of LARGE vs array index LARGE
Created:
7 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var myArrayLarge = Array(10000); for (let i=0; i<10000; i++) { myArrayLarge[i] = i; } console.log("setup complete!");
Tests:
for element of LARGE
let myArr = [] for (ele of myArrayLarge) { if (myArr.length > 300) { continue; } myArr.push(ele); }
for const element of LARGE
let myArr = [] for (const ele of myArrayLarge) { if (myArr.length > 300) { continue; } myArr.push(ele); }
array index LARGE
let myArr = new Array(300) for (let i=0; i<300; i++) { let something = myArrayLarge[i]; myArr[i] = myArrayLarge[i]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for element of LARGE
for const element of LARGE
array index LARGE
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.1:latest
, generated one year ago):
Let's break down the benchmark test cases. **What is being tested?** The test case is measuring the performance of iterating over an array with 10,000 elements using different methods. **Test Case 1: "for element of LARGE"** This test case uses a `for...of` loop to iterate over the large array (`myArrayLarge`). Inside the loop, it checks if the length of another array (`myArr`) is greater than 300. If true, it skips the current iteration using the `continue` statement. Otherwise, it pushes each element from `myArrayLarge` into `myArr`. **Test Case 2: "for const element of LARGE"** This test case is identical to Test Case 1, except that it uses a `const` declaration for the loop variable (`ele`) instead of `let`. **Test Case 3: "array index LARGE"** This test case uses a traditional `for` loop with an index (`i`) to iterate over the large array. It creates another array (`myArr`) with a length of 300 and then iterates over the indices of `myArrayLarge`, copying each element into `myArr`. **What options are compared?** The test cases compare three different approaches for iterating over an array: 1. **`for...of` loop**: Test Case 1 and 2 use this approach, which is a modern JavaScript syntax introduced in ECMAScript 2015 (ES6). 2. **Traditional `for` loop with index**: Test Case 3 uses this approach, which has been available since the early days of JavaScript. **Pros and cons** Here's a brief summary: * **`for...of` loop**: + Pros: Modern syntax, concise code. + Cons: May be slower due to additional overhead (see Benchmark Results). * **Traditional `for` loop with index**: + Pros: Fast execution, low overhead. + Cons: More verbose code. **Benchmark Results** The results show that the traditional `for` loop approach is significantly faster than both `for...of` loop approaches. The difference in executions per second between the two `for...of` loops is about 4 times! **Other considerations** * **Browser performance**: These results are specific to Google Chrome 67, which might not reflect performance on other browsers. * **Array size**: The test uses a large array (10,000 elements). Results may vary for smaller or larger arrays. **Alternative approaches** If you need to iterate over an array in JavaScript, consider using the traditional `for` loop approach, especially if performance is critical. If you prefer modern syntax, use the `for...of` loop, but be aware of potential performance implications. Note that these test cases do not use any external libraries or special JS features. The code is plain JavaScript.
Related benchmarks:
array iteration Large2
array iteration Large - break
some vs for..of (with condition)
pushing large array vs destructuring
Comments
Confirm delete:
Do you really want to delete benchmark?