Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
zzHasOwn2
(version: 0)
Object lookup performance
Comparing performance of:
undefined vs typeof vs in vs hasOwn
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { a: 1, b: 2, d: 123 };
Tests:
undefined
undefined !== obj.d;
typeof
'undefined' !== typeof obj.d;
in
'd' in obj;
hasOwn
Object.hasOwn(obj, 'd');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
undefined
typeof
in
hasOwn
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 break down the provided benchmark definition and test cases. **Benchmark Definition** The benchmark is designed to measure object lookup performance. The script preparation code creates an object `obj` with properties `a`, `b`, and `d`. The objective is to compare different ways of checking if a property exists in this object. **Options Compared** There are four options being compared: 1. **`undefined !== obj.d;`**: This checks if the value of `obj.d` is not equal to `undefined`. 2. **`'undefined' !== typeof obj.d;`**: This checks if the type of `obj.d` (i.e., its string representation) is not equal to `'undefined'`. Note that this uses the `typeof` operator, which returns a string indicating the type of the variable. 3. **`'d' in obj;`**: This uses the `in` operator to check if the property `'d'` exists in the object `obj`. 4. **`Object.hasOwn(obj, 'd');`**: This uses the `hasOwn` method of the `Object` prototype to check if the property `'d'` is an own property of the object `obj`. **Pros and Cons** 1. **`undefined !== obj.d;`**: * Pros: Simple and efficient, as it only checks for equality with `undefined`. * Cons: May not work correctly for non-boolean values that are considered "falsey" in a boolean context (e.g., 0, null, undefined). 2. **`'undefined' !== typeof obj.d;`**: * Pros: Works for all types of variables, including those with custom string representations. * Cons: May be slower due to the overhead of the `typeof` operator and converting the value to a string. 3. **`'d' in obj;`**: * Pros: Simple and efficient, as it only checks for existence without comparing values. * Cons: May not work correctly if the property name is not a string or if there are multiple properties with the same name (e.g., object literals). 4. **`Object.hasOwn(obj, 'd');`**: * Pros: Works correctly even when the property name is not a string or if there are multiple properties with the same name. * Cons: May be slower due to the overhead of calling a method on the `Object` prototype. **Library and Special JS Features** The only library used in this benchmark is the `Object` prototype, which provides methods like `hasOwn`. There are no special JavaScript features or syntax used in this benchmark. **Other Alternatives** Some alternative ways to check if a property exists in an object might include: * Using `in` with a regex: `if (obj['d'] && /^x$/.test(obj['d']))` * Using a library like Lodash's `has` However, the most straightforward and efficient way is likely to use one of the options listed above (`undefined !== obj.d;`, `'undefined' !== typeof obj.d;`, `in`, or `Object.hasOwn`), depending on the specific requirements and performance considerations.
Related benchmarks:
abcddef
Temp obj or in check
Test on isEqual and fastEqual false performance
Lodash extend vs Object.assign
Deep merge lodash vs ramda vs deepmerge vs Immutable
Comments
Confirm delete:
Do you really want to delete benchmark?