Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
instanceof vs typeof franco
(version: 0)
Comparing performance of:
instanceof vs typeof vs test attribute
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = {a:2};
Tests:
instanceof
obj instanceof Object
typeof
typeof obj === 'object'
test attribute
obj.a
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
instanceof
typeof
test attribute
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
instanceof
4713475.0 Ops/sec
typeof
8190906.5 Ops/sec
test attribute
8364965.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark test cases and explain what's being tested, compared, and their pros and cons. **Benchmark Test Cases:** The benchmark test cases are designed to compare three different approaches: 1. `instanceof` check 2. `typeof` operator with a string literal (`"object"`) Each test case has a unique "Test Name" associated with it, which will help identify the specific scenario being tested. **Options Compared:** * `instanceof` vs `typeof` + In the first test case (`obj instanceof Object`), we're testing the performance of using the `instanceof` operator to check if an object is an instance of a particular constructor (in this case, `Object`). This checks for tightness and whether the object's prototype chain matches the expected prototype. + In the second test case (`typeof obj === 'object'`), we're testing the performance of using the `typeof` operator with a string literal to check if an object is an instance of `Object`. This can lead to false positives for primitive values (e.g., `NaN`, `-0`, or `true`) that are also "objects". * Accessing an attribute (`obj.a`) + In this test case, we're testing the performance of accessing a property on the `obj` object. This checks the overhead of property access and whether it has any notable impact on performance. **Pros and Cons:** 1. `instanceof` check: * Pros: + Can detect inheritance relationships and prototype chain mismatches. + May be faster for objects that don't inherit from `Object`. * Cons: + Can lead to false positives with primitive values that are also "objects". + May incur higher overhead due to the need to inspect the object's prototype chain. 2. `typeof` operator with string literal (`"object"`): * Pros: + Less overhead compared to using `instanceof`, as it only checks for a specific type and doesn't need to follow the prototype chain. * Cons: + May lead to false positives or incorrect results if used with primitive values that are also "objects". 3. Accessing an attribute (`obj.a`): * Pros: + A simple, straightforward test case that can be easily reproduced and compared. * Cons: + Limited in its scope as it only tests property access without considering other factors like prototype chain management. **Library:** The `Object` constructor is used in the benchmark, which is a built-in JavaScript library that provides an object creation function. Its purpose is to create new objects with a specified prototype, allowing for inheritance and prototypal inheritance. **Special JS Feature/Syntax:** There are no special JavaScript features or syntaxes mentioned in this benchmark. However, it's worth noting that the use of `instanceof` and `typeof` operators can be affected by other factors like object freeze, sealer, or proxy optimizations, which might not be explicitly tested here. **Alternatives:** Other alternatives for these tests could include: 1. Using a different type of operator (e.g., `constructor.name === 'Object'`) to check if an object is an instance of a particular constructor. 2. Implementing custom functions or methods to detect inheritance relationships and prototype chain mismatches. 3. Testing property access with other data types, like arrays or functions. Keep in mind that these alternatives might not provide the same level of insight as using `instanceof` and `typeof`, but they could still be interesting variations on the benchmark tests.
Related benchmarks:
instanceof vs typeof vs fast typeof object
instanceof vs typeof for objects
Check object. typeof vs constructor
instanceof vs typeof gyuguyguy
Comments
Confirm delete:
Do you really want to delete benchmark?