Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Fill up Set from array
(version: 0)
Create new set from array of data. What is faster constructor or iteration.
Comparing performance of:
Constructor vs Loop
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var stuff = Array(1000).fill('tag').map((item, index) => item + index)
Tests:
Constructor
var set = new Set(stuff)
Loop
var set = new Set() for (let i=0; i < stuff.length; i += 1) { set.add(stuff[i]) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Constructor
Loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark on MeasureThat.net compares two methods for creating a `Set` object in JavaScript: using the constructor and iterating through an array to add elements one by one. **Options Compared:** 1. **Constructor (`var set = new Set(stuff)`)**: This method directly creates a `Set` object, passing the entire `stuff` array as input. The `Set` constructor efficiently handles adding all elements from the array in a single operation. 2. **Looping (`var set = new Set(); for (let i=0; i < stuff.length; i += 1) { set.add(stuff[i]) }`)**: This method creates an empty `Set` object and then uses a `for` loop to iterate through the `stuff` array, adding each element individually to the set using the `set.add()` method. **Pros and Cons:** * **Constructor**: * **Pros**: Generally faster as it performs the addition of all elements in a single operation. This can be significantly more efficient than iterating through an array multiple times. * **Cons**: Can potentially consume more memory during the initial creation if the array is large, as it needs to store all the elements at once. * **Looping**: * **Pros**: More gradual memory usage – the `Set` object grows incrementally with each iteration. Might be slightly more readable for simpler use cases. * **Cons**: Generally slower because of the overhead of looping through the array and performing individual additions to the set. **Other Considerations:** * **Set Functionality**: Sets in JavaScript are designed to store unique values, automatically discarding duplicates. This is a key reason why using a `Set` is often preferred over an array when you need to avoid duplicate entries. * **Data Size**: The performance difference between the two methods might become more pronounced as the size of the input array increases. **Alternatives:** While not directly compared in this benchmark, other options exist for working with unique values: * **Using `Array.filter()`**: You could filter out duplicate elements from an array using `Array.filter()`. However, this doesn't offer the same efficiency or automatic uniqueness guarantee as a `Set`. * **Object Properties**: If you have a relatively small number of unique values, you could use object properties as a simple way to track them (though this wouldn't be ideal for large datasets). Let me know if you have any more questions!
Related benchmarks:
Object spread vs New map with string keys
make map with entries vs for of
new Map vs Array.from vs spread operator
new Map vs set array to map
Comments
Confirm delete:
Do you really want to delete benchmark?