Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
hasOwn vs hasOwnProperty vs typeof
(version: 0)
Object lookup performance
Comparing performance of:
typeof vs hasOwn vs hasOwnProperty
Created:
3 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:
typeof
typeof obj.d !== "undefined";
hasOwn
Object.hasOwn(obj, 'd')
hasOwnProperty
obj.hasOwnProperty( 'd' );
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
typeof
hasOwn
hasOwnProperty
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
typeof
152626352.0 Ops/sec
hasOwn
81012992.0 Ops/sec
hasOwnProperty
96256208.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested, compared options, pros and cons of each approach, and other considerations. **What is being tested?** The benchmark is testing the performance of three different methods for checking if a property exists in an object: 1. `typeof` operator 2. `Object.hasOwn()` method 3. `obj.hasOwnProperty()` method Each method checks if the property "d" exists in the object created by the script preparation code. **Options compared** The benchmark is comparing the performance of these three methods: * `typeof`: Returns a string indicating the type of the value (in this case, "undefined" for properties that don't exist). * `Object.hasOwn()`: A method that checks if an object has a property with the specified name. * `obj.hasOwnProperty()`: A method that checks if an object has a property with the specified name. **Pros and cons of each approach** 1. **`typeof` operator**: * Pros: Lightweight, fast, and widely supported (most browsers). * Cons: Returns a string, which might be slower due to string processing. 2. **`Object.hasOwn()` method**: * Pros: Explicit, easy to read, and more intuitive than `typeof`. * Cons: Might be slower due to the overhead of calling a method on an object. 3. **`obj.hasOwnProperty()``` method**: * Pros: Lightweight, fast, and widely supported (most browsers). * Cons: Requires accessing the `hasOwnProperty()` property of the object, which might incur a small overhead. **Other considerations** * Both `Object.hasOwn()` and `obj.hasOwnProperty()` methods can be slower than `typeof` because they involve checking if an object has a certain property. * The performance difference between these methods is likely to be negligible for most use cases. * It's worth noting that modern JavaScript engines (V8, SpiderMonkey) are quite fast at resolving property access and will typically outperform these benchmark tests. **Library used** None of the test cases use any external libraries. They only rely on built-in JavaScript features and objects. **Special JS feature or syntax** There is no special JavaScript feature or syntax being tested in this benchmark. It's a straightforward comparison of three simple methods for checking property existence. If you're interested in exploring other benchmarking options, here are some alternatives: 1. **Microbenchmarks on jsPerf**: A popular online platform for running microbenchmarks. 2. **Benchmarking libraries like Benchmark.js or speedtest-benchmark**: Can help with more complex benchmarking scenarios and provide detailed results. 3. **Built-in browser performance testing tools**: Many modern browsers have built-in tools for testing performance, such as Chrome's DevTools or Firefox's Performance Tool.
Related benchmarks:
undefined vs. typeof vs. in vs. hasOwnProperty
undefined vs. typeof vs. in vs. hasOwnProperty 2
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?