Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set has vs object key reversed
(version: 0)
performance comparison of ways to find if an array contains a value
Comparing performance of:
Includes vs Object[key] vs Set.has
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).reverse(); 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:
2 years ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 121 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Includes
369331.2 Ops/sec
Object[key]
5299131.0 Ops/sec
Set.has
4516372.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 Definition** The provided JSON represents a benchmark definition, which defines three test cases: 1. `Set has vs object key reversed`: This test case compares the performance of three approaches to check if an array contains a value. * Approach 1: `array.includes('sausage')` * Approach 2: `obj['sausage']` (accessing an object property using a bracket notation) * Approach 3: `set.has('saudage')` (checking if a Set contains a value using the `has()` method) 2. `Includes`: This test case measures the performance of the `array.includes('sausage')` approach. 3. `Object[key]`: This test case measures the performance of the `obj['sausage']` approach. **Options Compared** The three approaches compared in the first test case are: * **Approach 1: `array.includes()`**: This method checks if a specified value is present in the array. It uses binary search to find the value, which has an average time complexity of O(log n). * **Approach 2: `obj['sausage']`**: This method accesses an object property using bracket notation. The JavaScript engine needs to look up the property in the object's prototype chain and then perform a lookup in the object itself. This approach has a variable time complexity, depending on the number of properties in the object. * **Approach 3: `set.has()`**: This method checks if a specified value is present in the Set. It uses a hash table to store the elements and performs a constant-time lookup. The average time complexity of this approach is O(1). **Pros and Cons** Here's a summary of the pros and cons of each approach: * **Approach 1: `array.includes()`** + Pros: Fast and efficient, especially for large arrays. + Cons: May not be suitable for very small arrays or arrays with a limited number of unique values. * **Approach 2: `obj['sausage']`** + Pros: Can be useful when working with objects that have a specific structure. + Cons: Has a variable time complexity, which can lead to performance issues if the object is too large or has many properties. * **Approach 3: `set.has()`** + Pros: Fast and efficient, especially for sets with a large number of unique values. + Cons: May not be suitable for situations where you need to iterate over all elements in the set. **Library Usage** The benchmark uses the `Set` object, which is a built-in JavaScript object that stores unique values. The `has()` method is used to check if a value is present in the Set. **Special JS Features/Syntax** None of the benchmarks use any special JavaScript features or syntax. **Other Alternatives** If you're interested in exploring alternative approaches for checking if an array contains a value, here are some other options: * Using `indexOf()` method: This method returns the index of the specified value, or -1 if the value is not found. This approach has a time complexity of O(n), where n is the length of the array. * Using a custom loop: You can write a simple loop to iterate over the array and check for the presence of the specified value. This approach also has a time complexity of O(n). However, keep in mind that these alternative approaches may not be as efficient or scalable as the methods used in the benchmark.
Related benchmarks:
Set has vs object key
Object.keys(object).includes(key) vs key in object
array includes vs object key vs set
array find vs object key
Comments
Confirm delete:
Do you really want to delete benchmark?