Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set array expansion Part 3
(version: 0)
Tests the implications of expanding large sets into an array for finding values within them
Comparing performance of:
[...] vs Set.prototype.values() vs Array.from
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var mySet = new Set(); // Generate and add 10,000 unique items to the set for (let i = 0; i < 10000; i++) { mySet.add(i); }
Tests:
[...]
const setArray = [...mySet]
Set.prototype.values()
const setArray = mySet.values()
Array.from
const setArray = Array.from(mySet)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
[...]
Set.prototype.values()
Array.from
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
[...]
77148.5 Ops/sec
Set.prototype.values()
27478752.0 Ops/sec
Array.from
77360.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and explain what's being tested, compared options, pros and cons of each approach, and other considerations. **Benchmark Overview** The `Set` object is used to store unique values in JavaScript. The benchmark tests how different methods perform when expanding a large set into an array for finding values within it. The test cases compare the performance of three approaches: using the spread operator (`[...mySet]`), calling `values()` on the `Set` prototype, and using `Array.from(mySet)`. **Test Cases** 1. **Using the Spread Operator `[...mySet]`** * Purpose: Convert a `Set` object into an array. * Pros: + Simple and concise syntax. + Fast execution (average time: 20-30 ms for 10,000 elements). * Cons: + May not be the most efficient method, as it creates a new array and copies all elements. 2. **Calling `values()` on the `Set` prototype** * Purpose: Get an iterator over the values in the set, which can be converted to an array using `Array.from()`. * Pros: + More flexible than the spread operator, as it allows iterating over the set without creating a new array. + May be more efficient for large sets, as it avoids creating a new array. * Cons: + Requires calling the `values()` method on the `Set` prototype, which might be slower than using the spread operator directly. 3. **Using `Array.from(mySet)`** * Purpose: Convert a `Set` object into an array. * Pros: + More explicit and easy to understand than the spread operator. + May be faster than the spread operator for large sets, as it uses native code optimization. * Cons: + Less concise syntax compared to the spread operator. **Library: `Set`** The `Set` object is a built-in JavaScript data structure that stores unique values. Its purpose is to provide an efficient way to store and look up values in a set of unique elements. The `values()` method on the `Set` prototype returns an iterator over the values in the set. **Special JS Feature/Syntax: None** There are no special features or syntax used in this benchmark that require specific knowledge or expertise. **Other Alternatives** Alternative approaches to converting a `Set` object into an array include: * Using `mySetToArray()` function (not shown in the benchmark code) * Using a library like Lodash's `setToSafeArray()` * Implementing a custom conversion function using JavaScript's built-in methods, such as `forEach()` and `map()` Keep in mind that the performance difference between these alternatives may be negligible for small sets, but could become significant for large datasets. Overall, the benchmark provides a good understanding of how different approaches to converting a `Set` object into an array perform in terms of speed and efficiency.
Related benchmarks:
Array.from vs. ... expansion
set vs array iteration 100k elements
Set array expansion
Set array expansion Part 2
Comments
Confirm delete:
Do you really want to delete benchmark?