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:
8 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
gemma2:9b
, generated one year ago):
This benchmark tests different ways to iterate over a large array (10,000 elements) in JavaScript and selectively add elements to a smaller array until it reaches a size of 300. The code then compares the performance of three approaches: **Test Cases:** * **"for element of LARGE":** This uses a `for...of` loop, which is designed for iterating over iterable objects like arrays. It's considered modern and efficient. * **Pros:** Concise, readable, and often performs well. * **Cons:** Can be slightly slower than other methods if the array size is very large. * **"for const element of LARGE":** This uses a `for...of` loop with the `const` keyword for variable declaration. It's functionally identical to the previous test case. * **"array index LARGE":** This approach iterates over the array using a traditional `for` loop with an index (`i`) and directly accesses elements using their index. * **Pros:** Can sometimes be faster than loops that rely on iteration mechanisms like `for...of`. * **Cons:** Less readable, potentially more error-prone (due to potential for `index out of bounds` errors), and not as concise. **Benchmark Results:** As you can see from the results, the "array index LARGE" method performs the best in terms of executions per second. **Alternatives and Considerations:** * **Library Usage:** The benchmark doesn't use any external JavaScript libraries. Libraries like Lodash or Ramda could potentially offer optimized functions for array iteration. * **Algorithm Optimization:** Depending on your specific needs, you might explore different algorithms (like filtering) to achieve the desired result more efficiently. * **Profiling Tools:** If you need deeper performance insights, tools like Chrome's DevTools can help pinpoint bottlenecks within your code. Let me know if you have any other questions!
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?