Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lookup vs. include
(version: 0)
Comparing performance of:
includes vs lookup
Created:
6 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: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10, }
Tests:
includes
return a.includes(9)
lookup
return !!b[9]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes
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 benchmarking scenario. **What is being tested?** MeasureThat.net is testing two different approaches to check if a value exists in an array or object: `lookup` and `includes`. The test cases use JavaScript, which is being used to access elements in an array (`a`) and an object (`b`). **Options compared** The main difference between the two approaches lies in how they access the desired value: 1. **Lookup (!!b[9])**: This approach uses direct property access on the object `b`. It's equivalent to using `b.value` if `b` had a `value` property. The expression `!!b[9]` is true if the value at index 9 exists, and false otherwise. 2. **Includes (a.includes(9))**: This approach uses the `includes()` method on the array `a`. It returns true if the value 9 is found in the array, and false otherwise. **Pros and Cons** ### Lookup (`!!b[9]`) Pros: * Generally faster for accessing a single property * May be more efficient for objects with fewer properties Cons: * Can be less readable and maintainable, especially for complex object structures * Fails if the property name is not a valid identifier or if it's undefined ### Includes (`a.includes(9)`) Pros: * More readable and maintainable, as it clearly conveys the intention of searching for a value in an array * Can be more efficient for large arrays, as it uses a optimized algorithm to find the value Cons: * May be slower than direct property access for small objects or arrays * Not available in older browsers that don't support `includes()` **Library and Special Features** There is no library being used in this benchmark. However, it's worth noting that the use of double-bang (`!!`) to negate a boolean value is a common JavaScript pattern. **Other Alternatives** If you want to compare other approaches for checking if a value exists in an array or object, here are a few alternatives: * Using `in` operator: `b[9] in b` (note that this will return true even if the property name is not a valid identifier) * Using `hasOwnProperty()`: `b.hasOwnProperty(9)` (more robust than `in`, but still has its limitations) * Using regular expressions (`^` and `$`): `/^\d+$/.test(a[0])` or `/^\d+.*$/.test(b.value)` (regex can be slow and overkill for simple cases) Keep in mind that the best approach will depend on the specific use case, performance requirements, and target browser support.
Related benchmarks:
set.has vs. array.includes
array includes vs object key lookup
set.has vs. array.includes asdfasdf
set.has vs. array.includes first element
set.has vs. array.includes v22
Comments
Confirm delete:
Do you really want to delete benchmark?