Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For loop, length, spread set vs For of
(version: 0)
Comparing performance of:
For of vs Const length vs For loop
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var set = new Set(Array.from(Array(1000).keys()) .map((x) => `item--${x}`))
Tests:
For of
for (var val of set) { console.log(val); }
Const length
const arr = [...set] let length = arr.length for (let i = 0; i < length; i++) { console.log(arr[i]) }
For loop
const arr = [...set] for (let i = 0; i < arr.length; i++) { console.log(arr[i]) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
For of
Const length
For loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:120.0) Gecko/20100101 Firefox/120.0
Browser/OS:
Firefox 120 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
For of
299.8 Ops/sec
Const length
285.8 Ops/sec
For loop
263.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases. **Benchmark Overview** The benchmark is designed to compare the performance of three different approaches to iterating over an array in JavaScript: 1. Using `for...of` loop 2. Calculating the length of the array and then using a traditional `for` loop 3. Directly using a traditional `for` loop on the array **Test Case 1: For of** The first test case uses the `for...of` loop to iterate over the `set` array, which contains 1000 elements. * **Library:** None (native JavaScript feature) * **Pros:** + Concise and readable code + Easy to maintain and understand * **Cons:** + May have performance issues due to the overhead of creating and destroying iterators **Test Case 2: Const length** The second test case calculates the length of the `set` array using `Array.prototype.length` and then uses a traditional `for` loop to iterate over the elements. * **Library:** None (native JavaScript feature) * **Pros:** + Can be faster than using `for...of` due to the overhead of creating an iterator + Allows for caching the length, which can reduce overhead on subsequent iterations * **Cons:** + More code is required, making it less concise and more prone to errors **Test Case 3: For loop** The third test case uses a traditional `for` loop to iterate over the `set` array. * **Library:** None (native JavaScript feature) * **Pros:** + Fastest approach, as it avoids the overhead of creating an iterator + Allows for direct access to array elements using the index variable `i` * **Cons:** + Can be less readable and maintainable due to the need to manage the loop variables **Other Considerations** * **JavaScript features:** None are mentioned in this benchmark. * **Library usage:** Only native JavaScript libraries are used. **Alternatives** If you're looking for alternative approaches, consider the following: 1. Using `Array.prototype.forEach()`: This method provides a concise and readable way to iterate over arrays, but it may be slower than using traditional loops due to the overhead of creating an iterator. 2. Using `map()` or `filter()` with callback functions: These methods provide a concise way to process arrays, but they can be less efficient than using traditional loops due to the overhead of creating and destroying iterators. In conclusion, this benchmark provides valuable insights into the performance differences between various approaches to iterating over arrays in JavaScript. By understanding the trade-offs between conciseness, readability, and performance, developers can make informed decisions about which approach to use depending on their specific use case.
Related benchmarks:
unshift vs spread
Map convert
Array.from vs Spread (1000 numbers)
Array.from vs Spread using 1000000 elements / only counts conversion
Array.from vs Spread using 10000 elements / only counts conversion
Comments
Confirm delete:
Do you really want to delete benchmark?