Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. Object key lookup
(version: 0)
Comparing performance of:
includes vs key lookup
Created:
4 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}
Tests:
includes
return a.includes(9)
key 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
key lookup
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
82571984.0 Ops/sec
key lookup
145475824.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **Benchmark Overview** The provided benchmark compares two approaches to check if a value exists in an array: using the `includes()` method and direct key lookup on an object. The benchmark is designed to measure the performance difference between these two methods. **Options Compared** There are two options being compared: 1. **`includes()` Method**: This method checks if a specified value exists in an array by iterating through the elements of the array. 2. **Direct Key Lookup (Object)**: This approach directly accesses an object's property using its key, which is equivalent to checking if a key exists in an object. **Pros and Cons** **`includes()` Method** Pros: * More intuitive and readable code * Works with arrays of any type, not just primitive values * Can be used to check for membership in other data structures like sets or maps Cons: * May perform unnecessary work if the array is large and the value is not present * Can be slower than direct key lookup for small arrays or specific use cases **Direct Key Lookup (Object)** Pros: * Generally faster than the `includes()` method, especially for small arrays or objects * Can be optimized for specific use cases, like checking for existence in a large object Cons: * Requires knowledge of object properties and their behavior * May not be as readable or maintainable for complex data structures **Library/Functionality Used** In this benchmark, the `includes()` method is a built-in JavaScript function. Direct key lookup on an object is achieved using the syntax `obj[key]`, where `key` is the property name being accessed. **Special JS Feature/Syntax** None are explicitly mentioned in the provided benchmark definition or test cases. However, it's worth noting that this benchmark assumes a modern JavaScript engine and does not account for older engines or specific features like ES6+ syntax. **Other Alternatives** If you're interested in exploring alternative approaches to checking for membership in an array or object, here are a few options: * Using `Array.prototype.some()` method: This method checks if at least one element in the array meets a condition. * Using `Object.values()` and `Array.prototype.includes()`: This approach iterates through the values of an object and then uses `includes()` to check for presence. Keep in mind that these alternatives may have different performance characteristics or trade-offs compared to the original `includes()` method and direct key lookup approaches.
Related benchmarks:
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 lookup2
set.has vs. Object key in lookup
Comments
Confirm delete:
Do you really want to delete benchmark?