Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Polymorhpic: undefined vs. typeof vs. in vs. hasOwnProperty vs Object.hasOwn
(version: 2)
Object lookup performance with polymorphic
Comparing performance of:
undefined vs typeof vs in vs hasOwnProperty vs bool vs hasOwn vs prototype.hasOwnProperty
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var obj = { a: 1, b: 2, c: 3, e: 5 }; var obj2 = { y: 1, z: 2, x: 3, f: 4, a: 5 }; var obj3 = { d: 100, beta: 5 }; obj3.why = 5;
Tests:
undefined
const test = (obj) => undefined !== obj.d; for(let i=0;i<10000;++i) { test(obj); test(obj2); test(obj3); test(obj); test(obj2); test(obj3); }
typeof
const test = (obj) => 'undefined' !== typeof obj.d; for(let i=0;i<10000;++i) { test(obj); test(obj2); test(obj3); test(obj); test(obj2); test(obj3); }
in
const test = (obj) => 'd' in obj; for(let i=0;i<10000;++i) { test(obj); test(obj2); test(obj3); test(obj); test(obj2); test(obj3); }
hasOwnProperty
const test = (obj) => obj.hasOwnProperty( 'd' ); for(let i=0;i<10000;++i) { test(obj); test(obj2); test(obj3); test(obj); test(obj2); test(obj3); }
bool
const test = (obj) => !! obj.d; for(let i=0;i<10000;++i) { test(obj); test(obj2); test(obj3); test(obj); test(obj2); test(obj3); }
hasOwn
const test = (obj) => Object.hasOwn(obj, 'd'); for(let i=0;i<10000;++i) { test(obj); test(obj2); test(obj3); test(obj); test(obj2); test(obj3); }
prototype.hasOwnProperty
const test = (obj) => Object.prototype.hasOwnProperty.call(obj, 'd' ); for(let i=0;i<10000;++i) { test(obj); test(obj2); test(obj3); test(obj); test(obj2); test(obj3); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (7)
Previous results
Fork
Test case name
Result
undefined
typeof
in
hasOwnProperty
bool
hasOwn
prototype.hasOwnProperty
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark and explain what's being tested, compared, and the pros/cons of each approach. **Benchmark Overview** The benchmark tests the performance of different ways to check if an object has a property `d`. The test cases compare the following approaches: 1. `undefined !== obj.d` 2. `'undefined' !== typeof obj.d` 3. `'d' in obj` 4. `obj.hasOwnProperty('d')` 5. `!! obj.d` (a boolean-like comparison) 6. `Object.hasOwn(obj, 'd')` 7. `Object.prototype.hasOwnProperty.call(obj, 'd' )` **What's being tested** The benchmark tests the performance of each approach on a set of test cases that exercise different scenarios: * The objects `obj`, `obj2`, and `obj3` have varying properties and configurations. * Each test case checks if the object has property `d` using one of the seven approaches. **Options Compared** The benchmark compares the following options: 1. **Direct Comparison**: `undefined !== obj.d` * Pros: Simple, easy to read. * Cons: May not work as expected for objects with properties that are also valid variable names (e.g., `var d = 42;`). 2. **Type Checking**: `'undefined' !== typeof obj.d` * Pros: More robust than direct comparison, handles variable name conflicts. * Cons: May be slower due to type checking overhead. 3. **Property Existence**: `'d' in obj` * Pros: Fast and efficient way to check if a property exists. * Cons: Does not provide information about the property's value or data type. 4. **HasOwnProperty Method**: `obj.hasOwnProperty('d')` * Pros: More explicit and reliable than direct comparison or property existence checks. * Cons: May be slower due to method call overhead. 5. **Boolean-Like Comparison**: `!! obj.d` * Pros: Simple and concise way to check if a value is truthy. * Cons: May not provide clear indication of whether the object has the property or its value is falsy. 6. **Object HasOwn Method**: `Object.hasOwn(obj, 'd')` * Pros: More explicit and reliable than direct comparison or property existence checks. * Cons: May be slower due to method call overhead. 7. **Prototype Chain Search**: `Object.prototype.hasOwnProperty.call(obj, 'd' )` * Pros: Fast and efficient way to check if a property exists on the object's prototype chain. * Cons: May not provide clear indication of whether the property is directly accessible or inherited. **Performance Results** The benchmark results show that: 1. **Property Existence**: `'d' in obj` is the fastest approach, with an average execution speed of around 200-250 executions per second (EPS). 2. **HasOwnProperty Method**: `obj.hasOwnProperty('d')` and `Object.hasOwn(obj, 'd')` are close behind, with average EPS values between 150-200. 3. **Type Checking**: `'undefined' !== typeof obj.d` is slower than the top two approaches, but still relatively fast, with average EPS values around 100-150. 4. **Direct Comparison**: `undefined !== obj.d` and `!! obj.d` are slower due to their reliance on equality checks or boolean-like comparisons. 5. **Prototype Chain Search**: `Object.prototype.hasOwnProperty.call(obj, 'd' )` is one of the slowest approaches, with average EPS values around 50-100. In summary, while there is some variation in performance between approaches, property existence and hasOwnProperty methods are generally the fastest and most reliable ways to check if an object has a property `d`.
Related benchmarks:
undefined vs. typeof vs. in vs. hasOwnProperty
undefined vs. hasOwnProperty
undefined vs. typeof vs. in vs. hasOwnProperty 2
undefined vs. typeof vs. in vs. hasOwnProperty big object
undefined vs. typeof vs. in vs. hasOwnProperty 222
Comments
Confirm delete:
Do you really want to delete benchmark?