Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set has vs object keyx
(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:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = (new Array(20000)).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('sausage')
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:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:135.0) Gecko/20100101 Firefox/135.0
Browser/OS:
Firefox 135 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Includes
30638810.0 Ops/sec
Object[key]
814970560.0 Ops/sec
Set.has
816612928.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark, hosted on MeasureThat.net, compares the performance of three different approaches to check if an array contains a specific value: using `array.includes()`, accessing an object key (`obj['sausage']`), and checking a Set using `set.has()`. **Approaches Compared** 1. **Array Includes**: This approach uses the built-in `includes()` method on arrays, which checks for membership in the array. 2. **Object Key Access**: This approach accesses an element of an object directly using bracket notation (`obj['sausage']`). 3. **Set Has**: This approach uses a Set data structure to store unique values and checks for existence using `set.has()`. **Pros and Cons of Each Approach** 1. **Array Includes** * Pros: Simple, concise, and widely supported. * Cons: May have performance issues with large arrays due to the need to traverse the entire array. 2. **Object Key Access** * Pros: Fast, especially for small to medium-sized objects, as it directly accesses a specific property. * Cons: May not be suitable for very large objects or performance-critical scenarios, as accessing individual properties can be slower than other approaches. 3. **Set Has** * Pros: Fast and efficient, even for large numbers of unique values, as Sets use hash tables under the hood. * Cons: May require additional memory to store the Set, and may not be suitable for scenarios where the value is not unique. **Library and Syntax** The benchmark uses a custom script to generate an array of 20,000 objects with random properties. The `Object.keys()` method is used to get an array of object keys, which is then passed to the `Set` constructor to create a Set. No external libraries are required. **Special JavaScript Feature or Syntax** No special JavaScript features or syntax are mentioned in the benchmark definition. **Alternatives** Other alternatives for checking if an array contains a specific value include: 1. **Using `in` operator**: This approach uses the `in` operator to check if a property exists on an object. 2. **Using a regular expression**: This approach uses a regular expression with the `test()` method to search for a pattern in the array. 3. **Using a custom function**: This approach defines a custom function that iterates over the array and checks for membership. Each of these alternatives has its own pros and cons, which may vary depending on the specific use case and performance requirements.
Related benchmarks:
Set has vs object key
Object.keys(object).includes(key) vs key in object
array includes vs object key vs set
Set has vs object key 3
Comments
Confirm delete:
Do you really want to delete benchmark?