Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For loop with spread set vs for of Set
(version: 0)
Comparing performance of:
Foor loop vs for loop with 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:
Foor loop
for (var val of set) { console.log(val); }
for loop with 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
Foor loop
for loop with 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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36
Browser/OS:
Chrome 119 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Foor loop
268.2 Ops/sec
for loop with const length
286.9 Ops/sec
for loop
294.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark. **What is being tested?** The provided JSON represents a JavaScript microbenchmark that tests three different approaches for iterating over an array or set: 1. **For-of Set**: This approach uses the `for...of` loop to iterate over a set (in this case, created from an array). 2. **For loop with spread and length**: This approach creates an array copy from the original set using the spread operator (`[...]`) and then uses a traditional `for` loop to iterate over the array. 3. **Traditional For loop**: This is the most basic approach, where a traditional `for` loop is used to iterate over the array. **Options compared** The benchmark compares these three approaches in terms of execution speed. The results are stored in an array of objects, each containing metadata about the test run (browser, device platform, operating system, etc.). **Pros and Cons:** * **For-of Set**: This approach is concise and easy to read, as it leverages the `for...of` loop syntax. However, it may not be supported in older browsers or environments. * **For loop with spread and length**: This approach creates an array copy from the original set, which can lead to performance overhead due to the additional memory allocation and copying. However, it provides more control over iteration and is likely to be supported by most modern browsers and environments. * **Traditional For loop**: This approach is straightforward and easy to understand but may not be as concise or readable as the other two options. **Library usage:** There is no explicit library mentioned in the benchmark definition or test cases. However, it's worth noting that the `Set` data structure and array creation using the spread operator (`[...]`) are part of the modern JavaScript standard library. **Special JS features or syntax:** The benchmark uses the `for...of` loop syntax, which is a relatively recent feature introduced in ECMAScript 2015 (ES6). This syntax allows for more concise and expressive iteration over iterable objects like sets.
Related benchmarks:
Javascript string to array mapping: Array.from() vs Spread syntax [...spread]
Array from() vs Map.keys() vs Map.values() vs spread
Array from() vs Map.keys() vs Map.values() vs spread (fixed)
Array.from vs Spread (1000 numbers)
Array.from vs Spread using 10000 elements / only counts conversion
Comments
Confirm delete:
Do you really want to delete benchmark?