Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
actual set.has vs. Object lookup vs. key in Object
(version: 0)
Comparing performance of:
includes vs key lookup vs key in obj lookup vs set has
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; var b = {1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true} var c = new Set(a)
Tests:
includes
return a.includes(9)
key lookup
return !!b['9']
key in obj lookup
return '9' in b
set has
return c.has('9')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
includes
key lookup
key in obj lookup
set has
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 YaBrowser/24.1.0.0 Safari/537.36
Browser/OS:
Yandex Browser 24 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
13252210.0 Ops/sec
key lookup
12896126.0 Ops/sec
key in obj lookup
13168417.0 Ops/sec
set has
13532800.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON data and explain what's being tested. **Benchmark Definition** The benchmark definition is a set of four test cases that compare different approaches to check if a value exists in an array, object, or Set: 1. `set.has`: Checks if a value exists in a `Set` data structure. 2. `includes`: Uses the `includes()` method on an array to check if a value exists within it. 3. `key lookup`: Uses the bracket notation (`[]`) with a string key to access a value in an object, and then checks if the result is truthy (i.e., not null or undefined). 4. `key in obj lookup`: Similar to `key lookup`, but uses the `in` operator instead of bracket notation. **Options Compared** The benchmark compares four different approaches: 1. Using a `Set` data structure (`set.has`) 2. Using the `includes()` method on an array (`includes`) 3. Using bracket notation with a string key on an object (`key lookup`) 4. Using the `in` operator on an object (`key in obj lookup`) **Pros and Cons of Each Approach** Here's a brief summary of the pros and cons of each approach: 1. **Set.has**: Pros: Fast, efficient, and accurate. Cons: Requires creating a Set data structure, which may have overhead. 2. **includes**: Pros: Easy to implement, widely supported. Cons: May be slower than using a Set or bracket notation for large arrays. 3. **key lookup**: Pros: Efficient for small objects, easy to implement. Cons: May be slower for large objects due to the string lookup. 4. **key in obj lookup**: Similar pros and cons as `key lookup`. **Library Usage** None of the provided benchmark definitions use external libraries. **Special JavaScript Features/Syntax** The benchmark uses the following special features: 1. **Set data structure**: Used to store unique values for fast membership testing. 2. **Bracket notation** (`[]`): Used to access values in an object using a string key. 3. **In operator** (`in`): Used to check if a property exists in an object. **Alternatives** Other alternatives for checking existence in arrays, objects, or Sets include: 1. Using `Array.prototype.indexOf()` with a default value (e.g., `-1`) to return the index of the first occurrence. 2. Using `Object.keys()` and `indexOf()` to check if a key exists in an object. 3. Using `Map` data structures for fast membership testing. Keep in mind that each approach has its trade-offs, and the best choice depends on the specific use case, performance requirements, and personal preference.
Related benchmarks:
set.has vs. Object key lookup for real
set.has vs. Object key lookup for real without bang bang
set.has vs. Object key
set.has vs. Object key lookup2
set.has vs. Object key in lookup
Comments
Confirm delete:
Do you really want to delete benchmark?