Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set has vs key in object
(version: 1)
performance comparison of ways to find if an array contains a value
Comparing performance of:
Includes vs Object[key] vs Set.has
Created:
8 months 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')
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:
8 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36
Browser/OS:
Chrome 139 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Includes
151107840.0 Ops/sec
Object[key]
117166384.0 Ops/sec
Set.has
221975616.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 8 months ago):
The benchmark titled "Set has vs key in object" tests the performance of three different methods for determining if a specific value exists in a dataset constructed in various formats. Specifically, it compares: 1. **Array.includes**: This method checks if a specific value exists in an array. - **Test Case**: `array.includes('sausage')` - **Pros**: - Simple syntax. - Readable and self-explanatory. - **Cons**: - Linear time complexity O(n), meaning it will take longer on large arrays because it has to search through each element until it finds the match. 2. **Object[key]**: This method checks if a property with a specific key exists in an object. - **Test Case**: `'sausage' in obj` - **Pros**: - Fast access time due to the way JavaScript engines optimize object property lookups, generally O(1) average time complexity due to hashing mechanisms. - **Cons**: - Only checks for the existence of keys, not values. This means it isn't a direct match for all use cases where you need to check values. 3. **Set.has**: This method checks whether a `Set` contains a specific value. - **Test Case**: `set.has('sausage')` - **Pros**: - Optimized for fast lookups, O(1) average time complexity. - `Set` is specifically designed for unique values, reducing duplication and potentially saving memory. - **Cons**: - Requires the overhead of creating a `Set` from an array initially, which adds complexity and can affect performance during this setup phase. ### Library and Syntax Considerations - The benchmark utilizes JavaScript's native `Set` object, which is part of ECMAScript 2015 (ES6). A `Set` is a built-in object that stores unique values of any type, whether primitive values or references to objects. ### Performance Results In the benchmark results: - `Set.has` performed the best, with **221,975,616 executions per second**. - `Array.includes` followed with **151,107,840 executions per second**. - `Object[key]` performed the slowest, achieving **117,166,384 executions per second**. This order reflects the computational complexity associated with each method. ### Alternatives The benchmark primarily focuses on three specific strategies, but other alternatives might include: - **Filtering with `.filter()`**: While this method can achieve similar functionality, it's less optimized by design because `.filter()` constructs a new array, thus having poor performance for checking existence. - **Using a manual loop (for/while)**: This can provide more control and allow for early termination but sacrifices the conciseness of the other methods. - **Using libraries like Lodash**: Libraries like Lodash provide utility functions such as `_.includes()` but are generally less performant due to the overhead of additional abstractions. In summary, the benchmark effectively contrasts the performance of methods to find if a value exists, demonstrating that `Set.has` offers the best performance for lookups at the cost of slightly more complexity during the initial data structure setup. Understanding these options allows developers to choose the best approach based on their specific application needs.
Related benchmarks:
array includes vs object key
Set has vs object key
Set has vs object keyx
Set has vs object key reversed
Set has vs prop in object
array includes vs object key vs set
Set has vs object key 3
Set has vs object key vs Array includes (key is at the end)
Set has vs object key bigger list
Comments
Confirm delete:
Do you really want to delete benchmark?