Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
typeof vs instanceof vs constructor
(version: 0)
Comparing typeof vs instanceof vs .constructor property
Comparing performance of:
typeof check vs instanceof check vs constructor check
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function Ticket(id) { this._id = id; } var testTicket = new Ticket(1); var count = 1000;
Tests:
typeof check
for (var i = 0; i < count; i += 1) { if (typeof testTicket === 'object') { continue; } }
instanceof check
for (var i = 0; i < count; i += 1) { if (testTicket instanceof Ticket) { continue; } }
constructor check
for (var i = 0; i < count; i += 1) { if (testTicket.constructor.name === 'Ticket') { continue; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
typeof check
instanceof check
constructor check
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Browser/OS:
Chrome 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
typeof check
885966.2 Ops/sec
instanceof check
452259.1 Ops/sec
constructor check
94510.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview** The provided JSON represents a JavaScript benchmark test case, where the user compares the performance of three different approaches: `typeof`, `instanceof`, and `.constructor` property. **Benchmark Purpose** The benchmark tests how fast each approach can identify whether an object is a subclass of another object (in this case, `Ticket`) or not. The purpose of this test is to understand which approach is the most efficient in terms of performance. **Options Compared** There are three options compared: 1. `typeof`: This checks if the object is a constructor function using the `typeof` operator. 2. `instanceof`: This checks if the object is an instance of another object (in this case, `Ticket`) using the `instanceof` operator. 3. `.constructor.name === 'Ticket'`: This checks if the object's constructor has a property named `name` and its value is equal to `'Ticket'`. **Pros and Cons of Each Approach** 1. `typeof`: * Pros: Simple and straightforward. * Cons: May not work as expected for certain types (e.g., arrays, functions). 2. `instanceof`: * Pros: More accurate than `typeof` for checking subclass relationships. * Cons: Can be slower due to the overhead of the `instanceof` operator. 3. `.constructor.name === 'Ticket'`: * Pros: Fast and efficient. * Cons: Requires access to the object's constructor property, which may not be available in all situations. **Library Used** The test case uses the `Ticket` class, which is a simple class definition provided by the benchmark author. The purpose of this class is to serve as a representative object for testing the `instanceof` operator. **Special JS Feature or Syntax** There is no special JavaScript feature or syntax used in this benchmark except for the use of the `instanceof` operator, which is a built-in operator in JavaScript. **Other Considerations** * The test case uses a small sample size (1000 executions) to measure performance. * The benchmark runs on Firefox 131 and Windows desktop platforms. **Alternatives** If you wanted to compare these approaches using a different platform or browser, you could modify the `RawUAString` field in the benchmark result JSON to include additional information about the test environment. For example: ``` "RawUAString": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:131.0) Gecko/20100101 Firefox/131.0 Safari/537.36 Chrome/91.0.4472.124" ``` This would allow you to compare the performance of each approach on different platforms and browsers using MeasureThat.net.
Related benchmarks:
Check function. typeof vs constructor + null check
typeof vs instanceof Function
Check function. typeof vs constructor + null check II
typeof vs instanceof Function vs call
Check object. typeof vs constructor
Comments
Confirm delete:
Do you really want to delete benchmark?