Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.hasOwn vs 'in' performance v2
(version: 0)
comparing Object.hasOwn(obj, "foo") with "foo" in obj
Comparing performance of:
Object.hasOwn vs in
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
obj = {a: 42, b:66, c: 88}
Tests:
Object.hasOwn
Object.hasOwn(obj, 'b')
in
'b' in obj
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.hasOwn
in
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36 Edg/143.0.0.0
Browser/OS:
Chrome 143 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.hasOwn
82586784.0 Ops/sec
in
156282096.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark compares two ways to check if a property exists in an object: `Object.hasOwn` and the `"in"` operator (also known as the "has property" operator). **Options Being Compared** There are two options: 1. **`Object.hasOwn(obj, 'b')`**: This is the traditional way to check if a property exists in an object using the `Object.hasOwn()` method. 2. **"'b' in obj"`**: This uses the `"in"` operator to check if the property `'b'` exists in the object. **Pros and Cons of Each Approach** 1. **`Object.hasOwn(obj, 'b')`**: * Pros: More explicit and readable way to check for property existence. * Cons: May be slower due to method call overhead. 2. **"'b' in obj"`: * Pros: Faster since it's a simple operator evaluation. * Cons: Less explicit and may lead to ambiguity if not used correctly. **Library and Its Purpose** The `Object.hasOwn()` method is a built-in JavaScript method that checks if an object has a property with the given name. It's used to avoid the error "TypeError: Cannot read own property of undefined or null" when trying to access a non-existent property. **Special JS Feature/Syntax** There isn't any special JS feature or syntax being tested in this benchmark, as both approaches are standard JavaScript methods and operators. **Other Alternatives** If you were to compare other ways to check for property existence in an object, some alternatives could be: 1. **`in` operator with `typeof`**: e.g., `'b' in obj && typeof obj['b'] !== 'undefined'` 2. **Using a library or utility function**: Some libraries, like Lodash, provide functions like `hasOwnProperty()` that can check for property existence. **Benchmark Preparation Code** The script preparation code creates an object `obj` with three properties: `'a'`, `'b'`, and `'c'`. The values of these properties are hardcoded as numbers (42, 66, and 88). **Latest Benchmark Result** The latest benchmark result shows the execution rate per second for each browser on a desktop platform. The results suggest that the `"in"` operator is faster than `Object.hasOwn()` in this specific case. In summary, the benchmark compares two ways to check if a property exists in an object: `Object.hasOwn()` and the `"in"` operator. While both approaches have their pros and cons, the `"in"` operator appears to be slightly faster in this particular case.
Related benchmarks:
undefined vs. typeof vs. in vs. hasOwnProperty 2
hasOwnProperty string vs number
in vs hasOwn
in vs Object.hasOwn vs Object.prototype.hasOwnProperty
Comments
Confirm delete:
Do you really want to delete benchmark?