Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set has vs object key vs array include
(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 vs key in obj
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; }, { sausage: 'tst' }); var map = new Map(Object.entries(obj)) var array = Object.keys(obj); var set = new Set(array);
Tests:
Includes
array.includes('sausage') array.includes('notFound')
Object[key]
obj['sausage'] obj['notFound']
Set.has
set.has('sausage') set.has('notFound')
map.has
map.has('sausage') map.has('notFound')
key in obj
'sausage' in obj; 'notFound' in obj;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Includes
Object[key]
Set.has
map.has
key in obj
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/129.0.0.0 Safari/537.36
Browser/OS:
Chrome 129 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Includes
1332641.1 Ops/sec
Object[key]
14625378.0 Ops/sec
Set.has
15797105.0 Ops/sec
map.has
15849242.0 Ops/sec
key in obj
14158992.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Overview** The benchmark is designed to compare the performance of different ways to check if an array contains a value, including using object keys and sets. The test case generates a large array (1,000 elements) with random key-value pairs and then performs several operations: 1. Array `includes` method 2. Object lookup using square bracket notation (`obj[key]`) 3. Set `has` method 4. Map `has` method 5. Key existence check using the `in` operator **Comparison of Options** The benchmark compares four options: 1. **Array `includes` method**: This is a built-in JavaScript function that takes a single argument and returns `true` if the element is present in the array, or `false` otherwise. 2. **Object lookup using square bracket notation (`obj[key]`)**: This method uses the object's key-value pairs to look up the desired value. If the key is not found, it will return `undefined`. 3. **Set `has` method**: A set is a data structure that stores unique values. The `has` method takes a single argument and returns `true` if the element is present in the set, or `false` otherwise. 4. **Map `has` method**: Similar to the set, a map is a data structure that stores key-value pairs. The `has` method uses the keys to look up the desired value. **Pros and Cons** Here's a brief summary of each option: * **Array `includes` method**: + Pros: Simple, efficient, and widely supported. + Cons: May be slower for large arrays due to its indexing mechanism. * **Object lookup using square bracket notation (`obj[key]`)**: + Pros: Fast for small arrays or objects with many keys. + Cons: Slow for large arrays or objects with few keys, as it requires a dictionary lookup. * **Set `has` method**: + Pros: Fast and efficient for checking membership in sets. + Cons: May require more memory to store the set, depending on the size of the input array. * **Map `has` method**: + Pros: Similar performance characteristics to the set `has` method, with added benefits from caching keys. + Cons: Requires a map object to be created, which may consume additional memory. **Other Considerations** The benchmark also uses two additional methods: 1. **Key existence check using the `in` operator**: This method checks if a key exists in an object by using the `in` operator. It returns `true` if the key is present, and `false` otherwise. 2. **`Includes` method** (with `notFound` argument): This option is similar to the array `includes` method but with a specific test case for finding the absence of an element. The benchmark uses MeasureThat.net's built-in features to prepare the script and HTML code, which simplifies the testing process. The results are displayed in a table format, making it easy to compare performance across different options. **Library Usage** There is no explicit library usage mentioned in the benchmark definition or test cases. **Special JS Features/Syntax** The only special feature used here is the use of arrow functions (`=>`) and `reduce()` method in the script preparation code. However, these are standard JavaScript features and do not require any special knowledge or expertise to understand. If you have any specific questions about this benchmark or would like further clarification on any aspect, feel free to ask!
Related benchmarks:
Set has vs object key
Object.keys(object).includes(key) vs key in object
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?