Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
finding item in a structure
(version: 0)
looking things up
Comparing performance of:
includes vs has vs in
Created:
5 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 = new Set(a) var c = {1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true}
Tests:
includes
var rando = parseInt(Math.random() * 10); a.includes(rando);
has
var rando = parseInt(Math.random() * 10); b.has(rando)
in
var rando = parseInt(Math.random() * 10); rando in c;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
includes
has
in
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Definition JSON** The benchmark definition represents a JavaScript function that performs a lookup operation on an array or object. The script preparation code defines: 1. An array `a` containing 10 elements. 2. A Set `b` created from the elements of `a`. 3. An object `c` with keys matching the elements of `a`, where each key has a value of `true`. The HTML preparation code is empty, indicating that no HTML elements are involved in this benchmark. **Individual Test Cases** There are three test cases: 1. **"includes"`: This test case generates a random integer between 0 and 9 using `Math.random()` and checks if the resulting number exists in the array `a` using the `includes()` method. 2. **"has"`: Similar to the first test case, but uses the Set `b` instead of the array `a` for the lookup operation. 3. **"in"`: This test case generates a random integer between 0 and 9 and checks if it exists as a property in the object `c`. **Comparison** The three test cases compare the performance of: 1. The `includes()` method on an array (`a`) 2. The `has()` method on a Set (`b`) 3. The `in` operator on an object (`c`) **Pros and Cons of each approach:** * **`includes()`**: Pros: + Widely supported in modern browsers. + Can handle arrays with duplicate elements. * Cons: + May have higher overhead due to iteration over the array. * `has()`: Pros: + Efficient for Sets, as they use a hash table lookup. + Fast and lightweight. * Cons: + Only supported on modern browsers that support Sets (e.g., Chrome 87). + May not work well with large arrays or complex data structures. * **`in` operator**: Pros: + Efficient for objects, as it uses a property lookup. + Fast and lightweight. * Cons: + Only supported in modern browsers that support the `in` operator (e.g., Chrome 87). + May not work well with large arrays or complex data structures. **Library and Special JS features:** * The Set library is used in the benchmark definition. A Set is a collection of unique values, and it provides an efficient way to perform membership testing. * No special JavaScript features (e.g., async/await, Promises) are required for this benchmark. **Other Alternatives:** If you want to include alternative methods or libraries, such as: 1. **`indexOf()`**: Instead of `includes()`, use the `indexOf()` method on an array. 2. **`hasOwnProperty()`**: Instead of the `in` operator, use the `hasOwnProperty()` method on an object. 3. **Custom implementation**: Implement a custom lookup function using a data structure like a hash table or trie. Keep in mind that these alternatives may not be supported by all browsers or devices, and their performance might vary compared to the original test cases.
Related benchmarks:
set.has vs. Object
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 in lookup
Comments
Confirm delete:
Do you really want to delete benchmark?