Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs object key lookup vs array includes
(version: 0)
Comparing performance of:
includes vs key lookup vs set has
Created:
2 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']
set has
return c.has(9)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
includes
key lookup
set has
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 JavaScript microbenchmark on MeasureThat.net. **Benchmark Overview** The benchmark compares three different approaches to check if a value exists in an array or object: 1. `set.has()`: using a `Set` data structure 2. `object key lookup`: directly accessing an object property by its key 3. `array includes`: using the `includes()` method of an array **Options Compared** The benchmark compares the performance of these three approaches for checking if a value exists in an array or object. Pros and Cons: 1. **`set.has()`**: * Pros: Fast lookup times, can be more efficient than direct property access. * Cons: Requires creating a `Set` instance, which may incur additional overhead. 2. **`object key lookup`**: * Pros: Simple and straightforward approach, doesn't require creating an extra data structure. * Cons: May be slower than using a `Set`, as it involves direct property access and potential cache misses. 3. **`array includes()`**: * Pros: Fast and efficient, especially for large arrays. * Cons: May not perform well for small arrays or objects with sparse data. **Library Used** None in this benchmark. **Special JS Features/Syntax** The benchmark uses the `Set` data structure, which is a built-in JavaScript feature. The `includes()` method of arrays and the direct property access syntax (`b['9']`) are also standard JavaScript features. **Other Considerations** When choosing an approach, consider the specific use case: * For large datasets or performance-critical applications, using a `Set` might be the best choice. * For simple, straightforward checks, direct property access or `includes()` method might be sufficient. * For sparse arrays or objects, `includes()` method might perform better. **Alternatives** Other approaches to check if a value exists in an array or object include: 1. Using the `in` operator: `a['9']` 2. Using a custom implementation with binary search or hash table lookup 3. Using a third-party library like Lodash's `contains()` function These alternatives might offer different performance characteristics, trade-offs, or use cases compared to the approaches tested in this benchmark.
Related benchmarks:
set.has vs. Object key lookup 2
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?