Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set has vs object key
(version: 0)
performance comparison of ways to find if an array contains a value
Comparing performance of:
Includes vs Object[key] vs Set.has
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = (new Array(1000)).fill(null).reduce((prev, newVal) => {prev[Math.random() + ''] = Math.random() + ''; return prev; }, { sausage: 'tst' }); var array = Object.keys(obj); var set = new Set(array);
Tests:
Includes
array.includes('sausage')
Object[key]
obj['sausage']
Set.has
set.has('saudage')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Includes
Object[key]
Set.has
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:146.0) Gecko/20100101 Firefox/146.0
Browser/OS:
Firefox 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Includes
24313642.0 Ops/sec
Object[key]
751150784.0 Ops/sec
Set.has
755223808.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested. **Benchmark Definition JSON** The benchmark definition represents three different ways to check if an array contains a specific value: 1. `Set.has('sausage')`: This method checks if a Set object contains the string 'sausage'. A Set is an unordered collection of unique values. 2. `obj['sausage']`: This method accesses the property 'sausage' on the `obj` object, which is created by reducing an array with null values and adding random keys. This approach relies on object key lookups. 3. `array.includes('sausage')`: This method checks if the string 'sausage' exists in the `array`. The array contains a large number of unique random strings. **Options Compared** The benchmark compares three different approaches: * **Set.has**: Using a Set data structure to check for membership. Sets are optimized for fast lookups and have an average time complexity of O(1). * **Object[key]**: Accessing the property on the object using bracket notation (`obj['sausage']`). This approach relies on object key lookups, which can be slower than using a Set. * **includes()**: Using the `array.includes()` method to check for membership in the array. This approach is generally faster than accessing individual elements of the array. **Pros and Cons** Here are some pros and cons of each approach: * **Set.has**: + Pros: Fast lookups, optimized for membership checks. + Cons: May not work as expected with non-unique values or objects that have duplicate keys. * **Object[key]**: + Pros: Works well with objects that have unique keys. + Cons: Can be slower than Set.has due to the overhead of property access and potential cache misses. * **includes()**: + Pros: Fast, efficient way to check for membership in arrays. + Cons: May not work as expected if the array contains non-unique values or is very large. **Library and Special JS Features** The benchmark uses a JavaScript library called YaBrowser/24 (Yandex Browser), which provides a specific implementation of WebKit-based browsing. There are no special JS features mentioned in this benchmark, so I won't discuss any further. **Other Alternatives** If you're looking for alternative ways to check if an array contains a value, you could consider using: * **Arrays.indexOf()**: This method returns the index of the first occurrence of the specified value. If the value is not found, it returns -1. * **Arrays.includes() with spread operator**: You can use the spread operator (`[...array].includes()` to check if an array contains a value. Keep in mind that these alternatives may have different performance characteristics and are not necessarily more efficient than the methods being compared in this benchmark.
Related benchmarks:
Object.keys(object).includes(key) vs key in object
Set has vs object key reversed
array includes vs object key vs set
Set has vs object key 3
Comments
Confirm delete:
Do you really want to delete benchmark?