Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set has vs object key vs map has
(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 Map.has
Created:
one year ago
by:
Registered User
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); var map = new Map(Object.entries(obj));
Tests:
Includes
array.includes('sausage')
Object[key]
obj['sausage']
Set.has
set.has('sausage')
Map.has
map.has('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
Map.has
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 YaBrowser/25.12.0.0 Safari/537.36
Browser/OS:
Yandex Browser 25 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Includes
106177992.0 Ops/sec
Object[key]
84597096.0 Ops/sec
Set.has
131166120.0 Ops/sec
Map.has
151075872.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its options. **Benchmark Definition** The main goal of this benchmark is to compare the performance of three methods for checking if an array contains a specific value: 1. `array.includes('sausage')` 2. `obj['sausage']` (using object key lookup) 3. `set.has('sausage')` (using Set has method) 4. `map.has('sausage')` (using Map has method) **Options Comparison** The options being compared are: * `array.includes()`: checks if a value exists in an array by iterating over the elements and checking for equality. * `obj['sausage']`: uses object key lookup to access a property of an object. In this case, the property is accessed using a random string as the key, making it unlikely to be present in most objects. * `set.has()`: checks if a value exists in a Set data structure by iterating over its elements and checking for equality. * `map.has()`: checks if a value exists in a Map data structure by accessing its entries and checking for existence. **Pros and Cons of Each Approach** 1. `array.includes()`: * Pros: widely supported, fast for small arrays. * Cons: slow for large arrays due to iteration over all elements. 2. `obj['sausage']`: * Pros: fast for objects with many properties, as it directly accesses a property using its key. * Cons: slow for objects without the desired property, and this benchmark uses a random property name, making it less likely to be present in most objects. 3. `set.has()`: * Pros: fast for large sets of unique values, as it uses a data structure optimized for lookups. * Cons: requires creating a Set instance, which can be slow if not necessary. 4. `map.has()`: * Pros: fast for maps with many entries, as it directly accesses an entry using its key. * Cons: requires creating a Map instance, which can be slow if not necessary. **Library and Purpose** In the provided benchmark script, the following libraries are used: * `Array.prototype.includes()`: part of the ECMAScript standard, providing a way to check for membership in an array. * `Object.keys()`, `Set`, and `Map`: built-in JavaScript objects that provide functionality for working with data structures. **Special JS Feature or Syntax** The benchmark uses a feature called "reducer" from the `Array.prototype` method. The reducer function is used to create an array of random strings, which is then used to generate a random object (`obj`). This allows the benchmark to test the performance of each option in a controlled environment. **Other Alternatives** Alternative methods for checking if an array contains a value could include: * `array.indexOf()` or `array.lastIndexOf()`: these methods search for the specified value from both ends of the array. * `array.includes()` with a custom iterator: this approach would allow testing more complex iteration patterns, but is not widely supported and may be slower than `includes()`. * Using a more specialized data structure, such as a Trie or a prefix tree, to store the values being searched for.
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 vs array include
Comments
Confirm delete:
Do you really want to delete benchmark?