Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set.toArray vs Spread
(version: 1)
Comparing performance of:
toArray vs Spread
Created:
one year ago
by:
Guest
Go to the latest result
Tests:
toArray
var fooSet = new Set(); for(var i=0;i<100;i++) { fooSet.add(i); } var other = fooSet.values().toArray();
Spread
var fooSet = new Set(); for(var i=0;i<100;i++) { fooSet.add(i); } var other = [...fooSet];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
toArray
Spread
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:145.0) Gecko/20100101 Firefox/145.0
Browser/OS:
Firefox 145 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Spread
288K/s
toArray
256K/s
View exact numbers
Test name
Executions per second
✓
Spread
287,655 Ops/sec
toArray
255,635 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark titled "Set.toArray vs Spread" evaluates two different methods for converting a `Set` object into an array in JavaScript. The two methods being compared are: 1. **Using the `Set.prototype.values()` method followed by calling `toArray()`.** 2. **Using the spread syntax `[...]` to convert the `Set` directly into an array.** ### Comparison of Options 1. **`Set.prototype.values().toArray()`** - **Pros**: - Clear intention: This method explicitly retrieves the values of the `Set`, making it clear that you are working with the elements of the `Set`. - **Cons**: - Requires an additional step of converting the iterable returned by `values()` into an array, which may add overhead. - The `.toArray()` method does not exist in standard JavaScript; this indicates a potential implementation that isn't widely recognized or is specific to certain contexts. If it does exist as part of a custom library or utility, it may not be available in all environments. 2. **Spread Syntax `[...fooSet]`** - **Pros**: - Concise syntax: The spread operator provides a succinct and readable way to convert an iterable to an array. - Built-in feature: The spread syntax is a part of standard JavaScript (ES6+) and thus widely supported and recognized across different environments, which ensures compatibility. - **Cons**: - None significant: The spread operator generally performs well and is recognized as an optimal way to construct arrays from iterable objects. ### Library Considerations In this benchmark, the `values()` method of the `Set` object is part of the ECMAScript standard and does not require any external libraries. However, the use of `.toArray()` suggests either a custom method or an experimental feature that may require a specific library or polyfill if not defined. As of the latest standards, JavaScript does not natively provide a `.toArray()` method for iterables. ### Special JavaScript Features Used The benchmark uses: - **Spread syntax (`[...]`)**: This is a prominent feature introduced in ES6 (ECMAScript 2015) that allows iterables (like arrays and sets) to be expanded into individual elements. The syntax increases code readability and expressiveness while maintaining performance. ### Alternatives While the benchmark focuses on two specific methods for converting `Sets` to arrays, there are other alternatives in JavaScript, including: - **Array.from()**: This method can convert array-like or iterable objects into arrays: ```javascript var arrayUsingArrayFrom = Array.from(fooSet); ``` - Pros: This method is inherently built into JavaScript and easily readable. - Cons: May perform slightly slower than using spread syntax in some scenarios, depending on the JavaScript engine. - **Looping through `Set`**: You could also manually loop through the `Set` and push each value into an array: ```javascript var arrayFromLoop = []; for (const item of fooSet) { arrayFromLoop.push(item); } ``` - Pros: Very flexible and customizable. - Cons: More verbose and error-prone than built-in methods. In conclusion, this benchmark provides insights into the performance of two common ways of converting a `Set` to an array in JavaScript. The spread syntax is generally preferred due to its conciseness, clarity, and widespread support, while the alternative method of using an undefined `.toArray()` is noteworthy for its potential limitations and dependency on external implementations.
Related benchmarks
Array.from vs Spread
Array.from vs Spread really
Array.from vs Spread (small)
Comments
Confirm delete:
Do you really want to delete benchmark?