Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array includes vs object key vs set
(version: 0)
performance comparison of ways to find if an array contains a value
Comparing performance of:
Includes vs Object[key] vs Set.has(key)
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]
obj['sausage']
Set.has(key)
set.has('sausage')
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(key)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Includes
15497112.0 Ops/sec
Object[key]
14744717.0 Ops/sec
Set.has(key)
15518599.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its test cases. **Benchmark Definition JSON** The provided JSON defines a benchmark that compares the performance of three different approaches to check if an array contains a value: 1. `array.includes('sausage')` 2. `obj['sausage']` (using object key lookup) 3. `set.has('sausage')` (using a Set data structure) **Script Preparation Code** The script preparation code creates a large object (`obj`) with 1000 properties, where each property has a random key and value. The keys are stored in an array (`array`), which is then converted to a Set (`set`). This setup ensures that the benchmark focuses on the performance of these specific operations. **Options Compared** The three test cases compare the following options: * `includes`: checks if a value exists in an array using the `includes` method. * `Object[key]`: uses object key lookup to access a property by its string representation (in this case, `'sausage'`). * `Set.has(key)`: uses the `has` method of a Set data structure to check if a key is present. **Pros and Cons** Here's a brief summary of each approach: 1. **`array.includes('sausage')`**: * Pros: Simple, widely supported, and relatively fast. * Cons: May perform a linear search on the array, which can be slow for large arrays. 2. **`obj['sausage']` (Object Key Lookup)**: * Pros: Fast, as it uses direct property access, but may require more memory to store the object. * Cons: Only works if the property name is a string literal; attempting to use a variable or expression will result in a syntax error. 3. **`set.has('sausage')` (Set Data Structure)**: * Pros: Fast and efficient, as Sets are optimized for membership testing. * Cons: May require additional memory to store the Set, and may not be suitable for large datasets. **Library/Functionality Used** None of these test cases use a specific library or framework. However, it's worth noting that the `includes` method is a built-in method in JavaScript arrays, so no external library is required. **Special JS Features/Syntax** There are no special JS features or syntax used in this benchmark. All three approaches rely on standard JavaScript language constructs. **Other Alternatives** For larger datasets or more complex scenarios, alternative approaches might include: 1. **Regular expressions**: Using a regular expression to search for the value can be faster than `includes` for large arrays. 2. **Index-based lookup**: If the array is sorted or has an index, using an index-based lookup (e.g., binary search) can be more efficient than `includes`. 3. **Native methods**: Depending on the browser and JavaScript engine, native methods like `Array.prototype.find()` or `Set.prototype.has()` might be available for even faster performance. Keep in mind that the specific approach chosen will depend on the use case and requirements of the application.
Related benchmarks:
array includes (worst case) vs object key
Set has vs object key
Object.keys(object).includes(key) vs key in object
array find vs object key
Comments
Confirm delete:
Do you really want to delete benchmark?