var obj = {};
typeof obj === "object"
obj.constructor === Object
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
typeof | |
constructor |
Test name | Executions per second |
---|---|
typeof | 13013205.0 Ops/sec |
constructor | 4764874.5 Ops/sec |
I'll break down the benchmark definition and test cases for you.
Benchmark Definition
The benchmark is defined by a JSON object that specifies two script preparation codes, an HTML preparation code (which is null in this case), and a list of individual test cases.
In essence, this benchmark measures the performance difference between two approaches:
typeof
operator on an empty object (obj = {};
) to determine its type.constructor
property of an empty object (obj = {};
) with the built-in Object
constructor.Options Compared
Two different approaches are compared:
typeof
operator to check if a variable is an objectconstructor
property of an object and comparing it to the Object
constructorPros and Cons of Each Approach
typeof
on certain types of objectsObject
constructor.constructor
property, which might be slower in some browsersLibrary or Special JS Feature
There is no library or special JavaScript feature used in this benchmark. It only relies on standard JavaScript syntax and built-in properties.
Other Alternatives
If you wanted to compare these approaches, you could also consider using other methods:
instanceof
check instead of typeof
lodash.isObject()
or Array.isArray()
Keep in mind that the performance difference between these approaches might be negligible in many cases, and other factors like browser version, engine implementation, and platform-specific optimizations might have a greater impact on performance.