var obj = { a: 1, b: 2, c: 3, d: 4, e: 5 };
if(undefined !== obj['d']){}
if('undefined' !== typeof obj['d']){}
if('d' in obj){}
if(obj.hasOwnProperty( 'd' )){}
if(!!obj['d']){}
if(obj['d']!==undefined){}
if(obj['d']!=undefined){}
if(obj['d']==undefined){}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
undefined | |
typeof | |
in | |
hasOwnProperty | |
bool | |
Undefined2 | |
undefined3 | |
undefined4 |
Test name | Executions per second |
---|---|
undefined | 3332628.8 Ops/sec |
typeof | 9414373.0 Ops/sec |
in | 9403065.0 Ops/sec |
hasOwnProperty | 7821605.5 Ops/sec |
bool | 8378716.0 Ops/sec |
Undefined2 | 3393731.2 Ops/sec |
undefined3 | 3363590.5 Ops/sec |
undefined4 | 3359387.8 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Overview
The benchmark measures the performance of different ways to check if a property exists in an object in JavaScript. The test cases compare the following approaches:
undefined
(directly checking for undefined
)typeof
(checking the type of the value using typeof
)in
(using the in
operator)hasOwnProperty
(specific to objects, checks if the property is a direct property of the object, not inherited from prototype chain)bool
(a boolean expression !!obj['d']
, which is true for non-empty strings and objects)undefined2
, undefined3
, undefined4
)Options Compared
The benchmark compares the performance of each approach across different devices (Desktop, Windows 10) and browsers (Chrome 78).
Pros and Cons of Each Approach
undefined
: This is the fastest approach, as it directly checks if a variable is defined or not. However, it may lead to unexpected results when used in certain contexts.typeof
: This method checks the type of the value using typeof
. While it's generally safe, it can be slower than direct comparison due to its overhead.in
: The in
operator checks if a property exists in an object or not. It's a simple and fast approach but may lead to unexpected results when used with objects that have inherited properties.hasOwnProperty
: This method is specific to objects and checks if the property is a direct property of the object, not inherited from prototype chain. While it's safer than in
, it can be slower due to its additional overhead.bool
: The boolean expression !!obj['d']
is true for non-empty strings and objects. It's a simple and fast approach but may lead to unexpected results when used with other types of values.Other Considerations
in
, it's essential to be aware that objects in modern JavaScript can have inherited properties, which might affect the performance.hasOwnProperty
method is not as efficient as direct comparison or typeof
, but it provides a safer alternative when working with objects.Alternative Approaches
Other approaches to check if a property exists in an object include:
Object.prototype.hasOwnProperty.call(obj, 'd')
Object.keys(obj).indexOf('d')
However, these approaches might be slower or more complex than the methods tested in this benchmark.