Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set has vs prop in object
(version: 0)
performance comparison of ways to find if an array contains a value
Comparing performance of:
Includes vs Object[key] vs Set.has vs Object.hasOwnProperty
Created:
2 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]
'sausage' in obj
Set.has
set.has('saudage')
Object.hasOwnProperty
obj.hasOwnProperty('sausage')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Includes
Object[key]
Set.has
Object.hasOwnProperty
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 Edg/134.0.0.0
Browser/OS:
Chrome 134 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Includes
120379384.0 Ops/sec
Object[key]
92248600.0 Ops/sec
Set.has
181424480.0 Ops/sec
Object.hasOwnProperty
71172752.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested, compared, and the pros/cons of each approach. **Benchmark Overview** The benchmark compares four different ways to check if an array contains a value: 1. `array.includes('sausage')` 2. `'sausage' in obj` (using the `in` operator on an object) 3. `set.has('saudage')` (using a Set data structure) 4. `obj.hasOwnProperty('sausage')` **Options Compared** Here's what's being compared: * `array.includes()`: checks if the array includes the specified value. * `in` Operator: checks if the object has a property with the specified key. * Set (`set.has()`) data structure: contains unique values and provides fast membership testing. **Pros/Cons of Each Approach** 1. **Array Includes**: This method is convenient, but it can be slow for large arrays because it needs to iterate through the entire array. However, for small arrays, it's usually fast enough. * Pros: easy to use, no need for additional data structures or complex logic. * Cons: can be slow for large arrays, may not be optimized for performance. 2. **`in` Operator**: This method is faster than `array.includes()` because it uses a hash table lookup. However, it requires an object and may be slower if the object is very large or has many properties. * Pros: fast, convenient when working with objects. * Cons: requires an object, may not work well for arrays or very large objects. 3. **Set (`set.has()`) data structure**: This method is optimized for fast membership testing and provides good performance for checking if a value is present in the set. * Pros: fast, efficient, easy to use with sets. * Cons: requires additional data structure (Set) and may not be necessary for small arrays or simple checks. **Library Used** None of these methods rely on any specific libraries beyond JavaScript's built-in features. However, the `in` operator uses a hash table lookup under the hood, which is implemented by the browser's JavaScript engine. **Special JS Features/Syntax** The benchmark doesn't use any special JavaScript features or syntax beyond the standard `includes()`, `in` operator, and Set data structure. **Other Alternatives** For arrays, other alternatives to `array.includes()` could be: * Using a binary search algorithm (e.g., `array.binarySearch()`). * Creating an index or map of array elements for fast lookup. For objects, other alternatives to the `in` operator could be: * Using `Object.keys()` and iterating through the resulting array. * Creating a map of object properties for fast lookup. For sets, other alternatives to `set.has()` could be: * Using a different data structure like a balanced binary search tree or a heap. Keep in mind that these alternatives may not provide better performance for specific use cases and might even introduce additional complexity.
Related benchmarks:
Set has vs object key
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?