Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array iteration Large2
(version: 5)
Comparing performance of:
for element of LARGE vs for const element of LARGE vs myArr forEach LARGE vs array index LARGE vs map LARGE vs forEach ES6 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; }
Tests:
for element of LARGE
for (ele of myArrayLarge) { let something = ele; }
for const element of LARGE
for (const ele of myArrayLarge) { let something = ele; }
myArr forEach LARGE
myArrayLarge.forEach(function(s) { let something = s; });
array index LARGE
for (let i=0; i<10000; i++) { let something = myArrayLarge[i]; }
map LARGE
myArrayLarge.map((i) => { let something = i; });
forEach ES6 LARGE
myArrayLarge.forEach(s => { let something = s; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
for element of LARGE
for const element of LARGE
myArr forEach LARGE
array index LARGE
map LARGE
forEach ES6 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 the performance of different ways to iterate over a large array in JavaScript. **Options Compared:** * **`for (ele of myArrayLarge)` (`for element of LARGE`)**: This uses the modern `for...of` loop, which iterates directly over elements in an iterable object like an array. It's concise and often considered more readable than other methods. * **`for (const ele of myArrayLarge)` (`for const element of LARGE`)**: Similar to the above but uses `const` to declare each element variable. This enforces that the variable cannot be reassigned, which can improve code clarity and performance in some cases. * **`myArrayLarge.forEach(function(s) { ... })` (`myArr forEach LARGE`)**: Uses the `.forEach()` array method, which takes a callback function to execute for each element in the array. This is a common pattern for simple operations on array elements. * **`for (let i=0; i<10000; i++) { ... }` (`array index LARGE`)**: A traditional `for` loop that iterates using an index to access each element by its position in the array. This method can be less readable than the other options. * **`myArrayLarge.map((i) => { ... })` (`map LARGE`)**: Uses the `.map()` array method, which creates a new array with the results of applying a provided function to each element in the original array. **Pros and Cons:** * **`for...of` and `forEach`**: Concise and readable, potentially performant. * **`map`**: Powerful for transforming arrays, but can be less efficient for simple iterations. * **`for` loop with index**: Can be explicit and allow fine-grained control over iteration, but often less readable and potentially slower than other methods. **Other Considerations:** * **Library Usage:** None of the test cases use libraries. The benchmark focuses on core JavaScript array iteration methods. * **Alternatives:** * **Async/Await for Large Datasets**: For extremely large arrays, asynchronous approaches using `Promise.all` or async iterators could be more efficient by processing elements in batches without blocking. * **Web Workers**: Offloading computationally intensive iterations to a Web Worker can improve the performance of the main thread, especially if the array is very large and operations are complex. Let me know if you have any more questions!
Related benchmarks:
array iteration Large - continue
Shifting array elements
some vs for..of (with condition)
pushing large array vs destructuring
Comments
Confirm delete:
Do you really want to delete benchmark?