Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set vs array
(version: 0)
Comparing performance of:
array vs set
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; var b = new Set(a)
Tests:
array
a.includes(10)
set
b.has(10)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array
set
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/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array
82069936.0 Ops/sec
set
143665728.0 Ops/sec
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark compares the performance of checking for the existence of an element in an array versus a Set using `includes` and `has` methods respectively. **Options Compared:** * **Array (`a.includes(10)`)**: Uses the `includes` method to check if the value `10` exists within the array `a`. * **Set (`b.has(10)`)**: Uses the `has` method to check if the value `10` exists within the Set `b`. **Pros and Cons:** * **Array (includes):** * **Pro:** Arrays are a more common data structure in JavaScript, so developers may be more familiar with using them. * **Con:** `includes` performs a linear search through the array, which can be slow for large arrays. * **Set (has):** * **Pro:** Sets utilize hash tables internally, enabling near-constant time complexity (O(1)) for checking element existence. This makes them significantly faster than arrays for this operation, especially with larger datasets. * **Con:** Sets have a more limited set of operations compared to arrays (e.g., they don't support indexing by position). **Other Considerations:** * **Use Case:** If you need fast membership checking and uniqueness is important, use a Set. If you require ordered data or specific array manipulation methods, an array might be more suitable. * **Dataset Size:** For small datasets, the performance difference between arrays and sets may be negligible. However, as the dataset size grows, the advantage of using a set becomes more pronounced. **Alternatives:** * **Using a Map:** Maps are key-value pairs similar to objects but offer faster lookups than standard JavaScript objects. If you need both membership checking and associated data values, Maps could be a good alternative. Let me know if you have any other questions!
Related benchmarks:
set vs array iteration
Includes (array) vs Has (Set)
Small n set vs array
set vs array find if exists
Comments
Confirm delete:
Do you really want to delete benchmark?