Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set has vs object key vs Array includes (key is at the end)
(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:
one year 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; }, {}); obj.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:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Includes
1345262.2 Ops/sec
Object[key]
18311396.0 Ops/sec
Set.has
20351868.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark in the JSON you provided compares three different approaches for checking if a collection contains a specific value (in this case, the string `'sausage'`). The test cases are: 1. **Array.includes()** 2. **Object[key]** 3. **Set.has()** ### Options Compared 1. **Array.includes('sausage')**: - This method checks if an array contains a certain value by iterating through the elements until it finds a match. It returns `true` if the value is found, and `false` otherwise. - **Pros**: - Simple and intuitive syntax. - Directly applicable to arrays, which are common structures in JavaScript. - **Cons**: - Can be slow for large arrays, as it performs a linear search (O(n) time complexity). - Requires iteration over each element until the target is found. 2. **Object[key]** (using a JavaScript object): - This method checks if a property (or key) exists in an object. Objects are collections of key-value pairs. - **Pros**: - Very fast key lookups (average O(1) time complexity). - Well-suited for situations where you need to access data by unique keys. - **Cons**: - Less semantic for collections of values as compared to arrays. - The user must be careful to ensure that the key exists, as accessing a non-existent key returns `undefined` rather than an error. 3. **Set.has('sausage')**: - This method checks if a `Set` contains a specific value. A `Set` is a collection of values where each value must be unique. - **Pros**: - Fast lookups (average O(1) time complexity) similar to objects. - Semantically clear for scenarios where you want to maintain a unique collection of values. - **Cons**: - Slightly more overhead in terms of memory compared to plain objects, as `Set` includes additional context for uniqueness. - May have a slightly higher initialization cost if the data structure is not reused. ### Benchmark Results Analysis The benchmark results from this test indicate the number of executions per second for each method, showing the efficiency of each approach: - **Set.has**: 20,351,868 executions/second – the fastest of the three in this scenario. - **Object[key]**: 18,311,396 executions/second – also very performant given the context. - **Array.includes**: 1,345,262.25 executions/second – the slowest, which aligns with its linear search nature. ### Other Considerations - **Memory Usage**: In high-performance applications, it is also important to consider the memory efficiency of these structures, especially when dealing with large datasets. - **Mutability**: Sets and objects are mutable collections. Arrays maintain their sequence, whereas sets do not guarantee any specific order. - **Data Structures**: Depending on the scenario, you may opt for more complex data structures such as Maps or additional libraries for specialized performance needs. ### Alternatives For scenarios where performance is critical, and the dataset is significant: - **Using Maps**: If you need to associate keys with values while preserving insertion order, `Map` can be an alternative with performance characteristics similar to `Set` and objects. - **Typed Arrays**: For numerical data, typed arrays may provide a performance boost. - **Third-party libraries**: Libraries like Lodash or Immutable.js offer a variety of utility functions that may help manage these data structures more efficiently. In conclusion, the choice between these methods largely depends on the requirements of your application regarding performance, memory usage, and semantic clarity.
Related benchmarks:
array includes vs object key
Set has vs object key
array includes vs object key (small)
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 include
Comments
Confirm delete:
Do you really want to delete benchmark?