Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
instanceof vs typeof vs !!value
(version: 0)
Comparing performance of:
instanceof vs typeof vs typeof no function vs !! value
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = {a:2};
Tests:
instanceof
obj instanceof Object
typeof
typeof obj === 'object'
typeof no function
typeof obj === 'object' && typeof obj !== 'function'
!! value
!!obj.a
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
instanceof
typeof
typeof no function
!! value
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
Browser/OS:
Chrome 129 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
instanceof
8906126.0 Ops/sec
typeof
22144772.0 Ops/sec
typeof no function
11832039.0 Ops/sec
!! value
22448668.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what is tested in the provided JSON and explain the different options compared, their pros and cons, and other considerations. **Benchmark Definition** The benchmark definition json specifies four test cases: * `instanceof obj instanceof Object`: Tests whether an object can be checked for prototype chain membership using the `instanceof` operator. * `typeof obj === 'object'`: Tests whether the result of the `typeof` operator is `'object'`. * `typeof obj === 'object' && typeof obj !== 'function'`: Tests whether the result of the `typeof` operator on an object that has a prototype chain is not `'function'`, and tests whether the result of the `typeof` operator on an object that does not have a prototype chain is `'object'`. * `!!obj.a`: Tests whether a bitwise NOT operator (`!!`) applied to the value of `obj.a` returns `true`. **Script Preparation Code** The script preparation code sets up a variable `obj` with a single property `a` having a value of 2. This object is used in all four test cases. **Html Preparation Code** There is no HTML preparation code specified, which means the benchmark only tests JavaScript-related functionality and does not consider any UI or DOM elements. **Options Compared** The benchmark compares the following options: * `instanceof` vs `typeof` + **Pros**: `instanceof` can be more efficient for checking prototype chain membership, while `typeof` is more general-purpose. + **Cons**: `instanceof` may not work correctly with objects that do not have a prototype chain (e.g., null or undefined). * `typeof obj === 'object'` + **Pros**: This test checks both the object type and the presence of a prototype chain. + **Cons**: The result can be influenced by the value of `obj`, which may not always indicate the intended functionality. * `typeof obj === 'object' && typeof obj !== 'function'` + **Pros**: This test checks the object type while excluding function types, providing more specific information about the object's nature. + **Cons**: The result can still be influenced by the value of `obj`, which may not always indicate the intended functionality. * `!!obj.a` + **Pros**: A simple and efficient way to check whether a value is truthy or falsy. + **Cons**: This test only checks one specific property, and the result does not provide information about the object's type or prototype chain. **Other Considerations** * The benchmark uses Chrome 129 as the browser, which may affect the results due to the specific implementation of the JavaScript engine in that version. * The `DevicePlatform` field indicates that the benchmark was run on a desktop device with Mac OS X 10.15.7. **Alternatives** Other alternatives for testing similar functionality include: * Using the `Object.prototype.toString.call()` method, which can provide more information about an object's type and prototype chain. * Using a library like Lodash, which provides functions like `isPlainObject()`, `isArray()`, etc., to test specific properties of objects. Keep in mind that these alternatives may have different performance characteristics or implications for code readability.
Related benchmarks:
instanceof vs typeof vs fast typeof object
instanceof vs typeof for objects
instanceof vs typeof franco
instanceof vs typeof vs !!
Comments
Confirm delete:
Do you really want to delete benchmark?