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 lookup
Created:
3 years ago
by:
Registered User
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 set = new Set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
Tests:
includes
return a.includes(9)
key lookup
return !!b['9']
set lookup
return set.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 lookup
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 JSON and explain what is being tested, compared, and analyzed. **Benchmark Definition** The benchmark definition represents the overall test suite that includes three individual test cases: 1. `Array.includes` 2. `Object key lookup` (using the dot notation `!!b['9']`) 3. `Set lookup` (using the `has()` method) Each test case is designed to measure the performance of a specific JavaScript operation on an array, object, or set. **Test Case Analysis** 1. **Array.includes** * The test case checks if the element `9` exists in the array `a`. This operation has a time complexity of O(n), where n is the length of the array. * The pros: simple and straightforward implementation. * Cons: can be slow for large arrays due to the linear search. 2. **Object key lookup** * The test case checks if the property `9` exists in the object `b`. This operation has a time complexity of O(1) on average, as it relies on hash tables under the hood. * The pros: fast and efficient for accessing individual properties by their keys. * Cons: may not be suitable for large datasets or complex objects due to memory constraints. 3. **Set lookup** * The test case checks if the element `9` exists in the set `set`. This operation has an average time complexity of O(1), as it uses a hash table to store and retrieve elements. * The pros: fast and efficient for accessing individual elements, especially when dealing with large datasets. * Cons: may not be suitable for sets with duplicate elements or complex operations. **Library and Special JS Feature** None of the test cases explicitly use any external libraries. However, JavaScript's built-in data structures (arrays, objects, and sets) are being used to perform the operations. There are no special JavaScript features mentioned in this benchmark definition.
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?