Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set has vs object key 2
(version: 0)
performance comparison of ways to find if an array contains a value
Comparing performance of:
Object[key] vs Set.has vs Includes (best) vs Includes (worst)
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_best_case = Object.keys(obj); var array_worst_case = Object.keys(obj).reverse(); var set = new Set(array_worst_case);
Tests:
Object[key]
obj['sausage']
Set.has
set.has('saudage')
Includes (best)
array_best_case.includes('sausage')
Includes (worst)
array_worst_case.includes('sausage')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Object[key]
Set.has
Includes (best)
Includes (worst)
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
Object[key]
7852281.5 Ops/sec
Set.has
6674695.0 Ops/sec
Includes (best)
8118156.0 Ops/sec
Includes (worst)
413059.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark and analyze what's being tested. **Benchmark Definition** The provided JSON defines a benchmark named "Set has vs object key 2" that compares the performance of three different approaches to check if an array contains a value: 1. Using an object as a key 2. Using a Set data structure 3. Using the `includes()` method on an array (in two scenarios: best and worst cases) **Script Preparation Code** The script preparation code generates an array of 1000 elements, each with a random string key and value. The keys are generated using the `reduce()` method, which creates a new object with a large number of properties. **Object[key] Approach** This approach uses the bracket notation (`obj['sausage']`) to access an element in the object. However, since the keys are randomly generated, this approach is not optimized for performance and is likely to be slower due to the overhead of string concatenation and lookup. Pros: Easy to implement and understand. Cons: Slow due to the overhead of string concatenation and lookup. **Set.has Approach** This approach uses a Set data structure to store the array elements. The `has()` method is then used to check if an element exists in the set. This approach is optimized for performance since it uses a hash table, which provides constant-time lookups. Pros: Fast due to the use of a hash table. Cons: May require additional memory to store the Set data structure. **Includes() Method Approach (Best and Worst Cases)** This approach uses the `includes()` method on an array to check if an element exists. The difference between the best and worst cases lies in how the array is indexed: * Best case: The array is indexed using a random number generator that generates values close to 0, making it easier for the browser's optimizer to predict the lookup. * Worst case: The array is indexed using a reverse iterator, which makes it harder for the browser's optimizer to predict the lookup. Pros: Best case: Fast due to the use of an optimized algorithm and caching. Worst case: Slow due to the overhead of the reverse iterator. Cons: Variable performance depending on the indexing strategy. **Library Usage** The benchmark uses the `Math.random()` function, which is a built-in JavaScript library. The `reduce()` method also uses this library. **Special JS Features or Syntax** This benchmark does not use any special JavaScript features or syntax that are not widely supported by modern browsers. **Other Alternatives** Alternative approaches to checking if an array contains a value include: * Using the `indexOf()` method, which returns -1 if the element is not found * Using a binary search algorithm, which has a time complexity of O(log n) * Using a data structure like a hash map or a trie, which can provide faster lookup times In conclusion, the benchmark tests the performance of three different approaches to check if an array contains a value: using an object as a key, using a Set data structure, and using the `includes()` method on an array. The pros and cons of each approach are discussed, along with potential alternatives for achieving faster lookup times.
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
Comments
Confirm delete:
Do you really want to delete benchmark?