Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
in vs. hasOwnProperty
(version: 0)
Object lookup performance
Comparing performance of:
in vs hasOwnProperty
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { a: 1, b: 2, c: 3, d: 4, e: 5 };
Tests:
in
'd' in obj;
hasOwnProperty
obj.hasOwnProperty( 'd' );
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
in
hasOwnProperty
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36
Browser/OS:
Chrome 142 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
in
81352872.0 Ops/sec
hasOwnProperty
48342864.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested, compared, and some pros/cons of each approach. **Benchmark Definition** The benchmark measures the performance of two different approaches to check if a key exists in an object: 1. **`'d' in obj`**: This uses the `in` operator to check if the string `'d'` is a property of the `obj` object. 2. **`obj.hasOwnProperty( 'd' )`**: This uses the `hasOwnProperty` method of the object, which checks if the specified property is an own property (i.e., not inherited from the prototype chain). **Options Compared** The two options are compared in terms of performance. **Pros and Cons of Each Approach** 1. **`'d' in obj`**: * Pros: Simple, efficient, and widely supported. * Cons: May be slower than `hasOwnProperty` for large objects or when the property is not found, as it requires a linear search through the object's properties. 2. **`obj.hasOwnProperty( 'd' )`**: * Pros: More accurate for large objects or when checking if a property is an own property. * Cons: May be slower than `in` due to the method call and additional checks. **Library Used** None, this benchmark uses native JavaScript features only. **Special JS Features/Syntax** Neither of these approaches uses any special JavaScript features or syntax. They are both part of the standard language. **Other Alternatives** If you're interested in exploring other options: * **`Object.prototype.hasOwnProperty.call(obj, 'd')`**: This is a similar approach to `hasOwnProperty`, but it relies on the `call` method and the prototype chain. * **`Array.isArray()` or `typeof` checks**: While not directly applicable to objects, these can be used to check if a value is an array or object, respectively. In general, for simple cases where performance is not critical, using `in` might be sufficient. However, when dealing with large objects or requiring more precision, `hasOwnProperty` or alternative approaches like `Object.prototype.hasOwnProperty.call(obj, 'd')` may be preferable.
Related benchmarks:
undefined vs. typeof vs. in vs. hasOwnProperty
undefined vs. typeof vs. in vs. hasOwnProperty big object
hasOwnProperty string vs number
in vs hasOwn
Comments
Confirm delete:
Do you really want to delete benchmark?