Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array includes vs key in object
(version: 2)
performance comparison of ways to find if an array contains a value
Comparing performance of:
Includes vs Object[key]
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
let menu = { width: 200, height: 300, title: "My menu" };
Tests:
Includes
menu.includes('title')
Object[key]
'title' in menu
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Includes
Object[key]
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Includes
0.0 Ops/sec
Object[key]
67490088.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark under consideration is a performance test comparing two distinct methods for determining if a specific value exists within an object in JavaScript, particularly focusing on the use of the `includes` method on arrays versus using the `in` operator on objects. ### Benchmark Overview - **Objective**: The goal of the benchmark is to measure the performance differences between `Array.prototype.includes()` and the `in` operator when checking if the string `'title'` exists in the given `menu` object. ### Options Compared 1. **Array.prototype.includes()**: - **Test Case**: `menu.includes('title')` - This method checks if an array contains a certain element. It's generally used with arrays. - **Pros**: - It returns a boolean value (`true` or `false`) indicating the presence of the value in the array. - It's more readable and expresses intent clearly when working with arrays. - **Cons**: - The method requires the use of an array. The benchmark's context is potentially misleading here since `menu` is an object, not an array. This is an incorrect usage of `includes`. - Generally, `includes` could be less performant for larger datasets because it traverses the entire array. 2. **in Operator**: - **Test Case**: `'title' in menu` - The `in` operator checks if a specified property exists in an object. - **Pros**: - It is specifically designed for objects, making it the right choice for checking if a property exists. - This approach is more efficient when dealing with property checks in objects since it directly looks up keys in the object structure. - **Cons**: - The syntax may be less immediately clear for those unfamiliar with JavaScript, as it diverges from standard function calls seen in methods. - It may return true even for properties in the prototype chain that are not in the object itself. ### Benchmark Results - **Test Results**: - For the `Object[key]` test with the `in` operator, the benchmark recorded an extraordinarily high execution speed of **67,490,088 executions per second**, indicating excellent performance. - On the contrary, the `includes` test resulted in **0.0 executions per second**, indicating that this usage either failed or was inappropriately applied, reflecting a lack of meaningful or proper outcome for this scenario. ### Other Considerations - **Correct Usage**: It's important to understand that using `includes` on an object like `menu` is invalid because `includes` is not a method of objects; rather, it is an array method. This emphasizes the importance of knowing the suitable data structure and method for specific tasks in JavaScript. - **Alternatives to Consider**: - For checking if a property exists in an object, alternatives include: - `Object.hasOwnProperty('propertyName')`: This method checks if the property exists on the object itself without looking into the prototype chain. - Using `Object.keys()` to retrieve an array of keys and then checking with `includes`, although this would generally be less efficient than the direct `in operator`. ### Conclusion This benchmark serves as an instructional comparison of appropriate JavaScript idioms for property existence checks. The invalid usage of `includes` on an object highlights the necessity to choose the right method based on the data structure being utilized. Understanding the performance implications and correct contexts for these operations is vital for software engineers to write efficient and effective code.
Related benchmarks:
IndexOf vs Includes
ddddadsad
Array.prototype.includes vs underscore.includes
array indexOf vs includes vs some v3
equality vs includes
Compare Or vs Includes
array includes vs object key (small)
=== vs includes
equals vs includes
Comments
Confirm delete:
Do you really want to delete benchmark?