Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set has vs object key 3
(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:
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 array = Object.keys(obj); var set = new Set(array);
Tests:
Includes
array.includes('sausage')
Object[key]
obj['sausage']
Set.has
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
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/126.0.0.0 Safari/537.36 Edg/126.0.0.0
Browser/OS:
Chrome 126 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Includes
16963538.0 Ops/sec
Object[key]
16097709.0 Ops/sec
Set.has
15704961.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, the options compared, pros and cons of each approach, library usage, special JavaScript features or syntax (if any), and alternative approaches. **Benchmark Definition** The benchmark consists of three test cases: 1. `array.includes('sausage')` 2. `obj['sausage']` 3. `set.has('sausage')` Each test case is a microbenchmark that measures the performance of a specific JavaScript operation. **Script Preparation Code** The script preparation code generates an object with 1000 properties, where each property has a random key and value. The keys are stored in an array using `Object.keys(obj)`. ```javascript var obj = (new Array(1000)).fill(null).reduce((prev, newVal) => {prev[Math.random() + ''] = Math.random() + ''; return prev; }, { sausage: 'tst' }); ``` This code creates a large object with random keys and values. The `sausage` property is specifically created to test the three different operations. **Options Compared** The benchmark compares the performance of three different ways to find if an array contains a value: 1. **Array includes**: Using the `includes()` method to check if a value exists in an array. 2. **Object key access**: Using the bracket notation (`obj['sausage']`) to access and retrieve a property from an object by its key. 3. **Set has**: Using the `has()` method of a Set data structure to check if a value exists. **Pros and Cons** Here's a brief overview of each approach: 1. **Array includes**: * Pros: Simple, widely supported, and efficient for large arrays. * Cons: Can be slower than other methods for small arrays or when the element is not present in the array. 2. **Object key access**: * Pros: Fast and efficient for objects with a fixed number of properties. * Cons: Slower for large objects or when the property is not found, as it involves object lookup and property access. 3. **Set has**: * Pros: Fast and efficient for large sets or when checking for membership in a set. * Cons: Requires a Set data structure, which may add overhead for small datasets. **Library Usage** The benchmark uses the `Set` data structure from the JavaScript built-in API. There are no external libraries used in this benchmark. **Special JavaScript Features/Syntax (None)** There are no special JavaScript features or syntax used in this benchmark beyond standard language constructs. **Alternative Approaches** Some alternative approaches for similar microbenchmarks might include: 1. **Using a sparse array**: Instead of creating an object with random keys, you could create a sparse array using `Array(1000).fill(null)` and then populate it with values. 2. **Using a Map data structure**: A Map can provide faster lookups compared to objects or Sets, especially for large datasets. Keep in mind that the choice of approach depends on the specific requirements and characteristics of your benchmark, as well as the intended use case for the results.
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?