Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set iterator vs Array.from
(version: 0)
Comparing performance of:
Iterate vs Spread
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
Iterate
var fooSet = new Set(); for(var i=0;i<100;i++) { fooSet.add(i); } let x = 0; for (const f of fooSet) { x++; }
Spread
var fooSet = new Set(); for(var i=0;i<100;i++) { fooSet.add(i); } let x = 0; for (const item of [...fooSet]){ x++; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Iterate
Spread
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 137 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Iterate
611027.3 Ops/sec
Spread
565282.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and analyzed. **Benchmark Purpose:** The primary goal of this benchmark is to compare the performance of two approaches for iterating over a set of numbers in JavaScript: 1. Using a traditional `for` loop with indexing (`fooSet.add(i)`) 2. Using the spread operator (`...`) to convert the Set into an array, followed by iterating using another `for` loop (`[...fooSet]`) **Options Compared:** The benchmark compares two options for iterating over a set of numbers: 1. **Traditional iteration**: The first option uses a traditional `for` loop with indexing (`fooSet.add(i)`). This approach is straightforward and easy to understand, but it may be slower due to the overhead of manual index management. 2. **Spread operator iteration**: The second option uses the spread operator (`...`) to convert the Set into an array, followed by iterating using another `for` loop (`[...fooSet]`). This approach creates a new array and then iterates over it. **Pros and Cons:** * **Traditional iteration (for loop with indexing)**: + Pros: - Easy to understand and implement - No additional memory allocation required + Cons: - May be slower due to manual index management - More prone to errors due to manual indexing * **Spread operator iteration**: + Pros: - Faster execution time (as shown by the benchmark results) - Less prone to errors, as the spread operator handles indexing automatically + Cons: - Requires additional memory allocation for the temporary array - May not be suitable for very large datasets due to potential memory constraints **Library and Special JavaScript Features:** There are no libraries used in this benchmark. However, it's worth noting that the use of the spread operator (`...`) is a relatively recent feature in JavaScript (introduced in ECMAScript 2015). It provides a convenient way to convert Sets into arrays and can improve performance in certain scenarios. **Benchmark Results:** The latest benchmark results show that the **spread operator iteration** approach outperforms the traditional `for` loop with indexing. This suggests that using the spread operator can provide a significant performance boost, especially for large datasets. **Other Alternatives:** In addition to the two options compared in this benchmark, other alternatives for iterating over a set of numbers might include: * Using `Array.from()` function, which creates an array from an iterable object (like a Set) * Using `forEach()` method on the Set object itself * Implementing a custom iterator or generator function
Related benchmarks:
Uint16Array.from() vs new Uint16Array()
Comparing Uint16Array.from() vs new Uint16Array()
Iterable toArray(): push() vs spread
Array.from() vs toString()
Array.from() vs new Array() with index
Comments
Confirm delete:
Do you really want to delete benchmark?