Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.includes vs. Object key lookup vs. Set.has
(version: 0)
Comparing performance of:
includes vs key lookup vs Set.has
Created:
4 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:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36
Browser/OS:
Chrome 139 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
130454528.0 Ops/sec
key lookup
246438320.0 Ops/sec
Set.has
254486272.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview** The provided benchmark measures the performance of three different approaches to check if a specific value exists in an array or object: `Array.includes()`, `Object key lookup` (using the `!!` operator), and `Set.has()`. **Tested Approaches** 1. **Array.includes()**: This method checks if a specified value (`9`) is present in the array `a`. 2. **Object Key Lookup**: This approach uses the `!!` operator to check if the key `'9'` exists in the object `b`. The `!!` operator converts its operand to a boolean value, where `true` becomes 1 and `false` becomes 0. Since the property '9' exists in the object, this expression evaluates to `!!true`, which is equivalent to 1 (truthy). In JavaScript, any non-zero value is considered true in a boolean context. 3. **Set.has()**: This method checks if the element (`9`) is present in the set `c`. **Pros and Cons of Each Approach** * **Array.includes():** * Pros: * Widely supported by modern browsers * Fast performance for large arrays (averaging O(n) time complexity) * Easy to use and understand * Cons: * Slower than `Set.has()` for very small sets or arrays with a single element * **Object Key Lookup:** * Pros: * Often faster than `Array.includes()` due to caching optimizations in modern browsers * Suitable for objects where the value is not just a boolean (e.g., using `in` operator) * Cons: * Less intuitive and harder to understand compared to `Array.includes()` * Not suitable for all use cases, as it can lead to unexpected behavior if not used correctly * **Set.has():** * Pros: * Fastest approach (averaging O(1) time complexity) * Suitable for sets where the element is only present once * Cons: * Less intuitive and harder to understand compared to `Array.includes()` * Requires a set data structure, which may not be necessary or efficient in all cases **Library: Set** The `Set` object is a built-in JavaScript data structure that stores unique values. It's implemented as an unordered collection of key-value pairs, where each value is associated with at most one key. In this benchmark, the set `c` contains the elements from array `a`. The `Set.has()` method checks if a specified element is present in the set. **Special JavaScript Feature: None** There are no special JavaScript features or syntax used in this benchmark beyond the standard methods and data structures discussed above. **Alternatives** Other alternatives for checking if an element exists in an array or object include: * **`in` operator**: Similar to `Object Key Lookup`, but uses a different syntax (`a['9']` instead of `!!b['9']`) and may have different performance characteristics. * **Regular expressions (regex)**: Can be used for pattern matching, but may not be the most efficient or intuitive approach in this specific use case. * **Custom implementation**: Depending on the specific requirements, you might need to implement a custom algorithm for checking if an element exists in an array or object.
Related benchmarks:
set.has vs. array.includes - including extra code for more fair comparison as typically you will have an array and not a set
Includes (array) vs Has (Set)
set.has vs. array.includes v22
array.includes vs. set.has on the fly
set.has (w/ creation) vs. array.includes
Comments
Confirm delete:
Do you really want to delete benchmark?