Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set array expansion Part 2
(version: 0)
Tests the implications of expanding large sets into an array for finding values within them
Comparing performance of:
[...] vs Set.prototype.values()
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()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
[...]
Set.prototype.values()
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
[...]
76975.5 Ops/sec
Set.prototype.values()
27686646.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided JSON represents a JavaScript benchmark test case for measuring the performance of two approaches to expand a Set object into an array. The benchmark test case is divided into three main parts: 1. **Script Preparation Code**: The script generates and adds 10,000 unique items to a Set object called `mySet`. 2. **Html Preparation Code**: This field is empty in this example. 3. **Benchmark Definition and Test Cases**: There are two test cases defined: * The first test case uses the spread operator (`[...]`) to expand the Set object into an array, assigning it to a variable named `setArray`. * The second test case uses the `values()` method of the Set prototype to expand the Set object into an iterator, and then converts it to an array using the `Array.from()` function or spread operator (`[...]`), assigning it to a variable named `setArray`. **Options Compared** The two test cases are comparing the performance of: * Using the spread operator (`[...]`) to expand a Set object into an array. * Using the `values()` method of the Set prototype and then converting it to an array. **Pros and Cons of Each Approach:** 1. **Spread Operator (`[...]`):** * Pros: + Simple and concise syntax. + Fast performance, as it creates a new array with the elements of the Set object. * Cons: + May not be as efficient for very large Sets, as it involves creating an intermediate array. 2. **Set.prototype.values() + Array.from():** * Pros: + Can handle larger sets without creating an intermediate array, as `values()` returns an iterator that yields elements one at a time. * Cons: + Requires calling the `Array.from()` function or spread operator (`[...]`) to create the final array, which may introduce additional overhead. **Library and Purpose** The Set object is a built-in JavaScript library that provides a data structure for storing unique values. The purpose of using a Set is to eliminate duplicates while maintaining fast lookup, insertion, and deletion operations. In this benchmark test case, the Set object is used to generate and store 10,000 unique items. The `values()` method of the Set prototype returns an iterator that yields each element in the set, allowing for efficient iteration over the elements without having to access the underlying array or memory allocation. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in this benchmark test case. However, note that the use of `mySet` and `setArray` variables is a common pattern in JavaScript benchmarking and testing, as it allows for easy setup and iteration over the data being tested. **Alternative Approaches:** Other approaches to expanding a Set object into an array might include: * Using `Array.from()` directly on the Set object, without calling `values()`: `const setArray = Array.from(mySet)` * Implementing a custom iterator or generator function to yield elements from the Set object * Using a library like Lodash's `setToArray()` function to convert a Set object into an array
Related benchmarks:
Array.from vs. ... expansion
Set array expansion
Set array expansion Part 3
Array.from vs Spread using 1000000 elements / only counts conversion
Comments
Confirm delete:
Do you really want to delete benchmark?