Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-of over sliced array
(version: 0)
Comparing performance of:
for-of normal loop vs for-of slice
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr1 = []; for(i=0; i<10000; i++){arr1.push(i);}
Tests:
for-of normal loop
for (const i of arr1) { i + 1; }
for-of slice
for (const i of arr1.slice(1)) { i + 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for-of normal loop
for-of slice
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):
I'd be happy to help explain the provided benchmark. **Benchmark Overview** The benchmark tests two approaches for iterating over arrays using `for...of` loops: 1. A normal `for...of` loop with no slicing of the array (`arr1`). 2. A `for...of` loop with slicing of the first element of the array (`arr1.slice(1)`). **Script Preparation Code** The script preparation code generates a large array `arr1` with 10,000 elements, and then initializes it by pushing numbers from 0 to 9,999 onto the array using a traditional `for` loop. **Options Compared** The benchmark compares two options: * A normal `for...of` loop: This approach iterates over the entire array in one pass. * A `for...of` loop with slicing of the first element of the array (`arr1.slice(1)`): This approach creates a new, smaller array by slicing off the first element from the original array. **Pros and Cons** **Normal `for...of` Loop:** Pros: * Can take advantage of optimizations in modern browsers for iterating over arrays. * May be faster due to fewer unnecessary operations (e.g., no creating a new array). Cons: * Still creates a new temporary array object during the slice operation. **For-of loop with Slicing:** Pros: * Creates a smaller, more contiguous array that can potentially reduce memory allocation and copying costs. * Can take advantage of browser optimizations for iterating over arrays with slicing. Cons: * May introduce additional overhead due to creating a new array and performing the slice operation. * Requires two passes through the original array (first to create the sliced array, then to iterate). **Library:** None **Special JS Feature/Syntax:** This benchmark does not use any special JavaScript features or syntax beyond the `for...of` loop. **Other Considerations** The benchmark assumes that the browser is running on a desktop platform with Safari 17. Other browsers and platforms may have different performance characteristics. **Alternatives** Other alternatives for iterating over arrays using `for...of` loops include: * Using `Array.prototype.forEach()` or other array methods, which can provide similar performance benefits to slicing. * Using traditional `for` loops, which can offer better control over the iteration process but may not take advantage of browser optimizations. It's worth noting that the choice of iteration approach ultimately depends on the specific use case and requirements of the application.
Related benchmarks:
Array clone
shallow copy of array
Array clone from index 1 to end: spread operator vs slice
slice vs new array
Comments
Confirm delete:
Do you really want to delete benchmark?