Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
instanceof vs constructor.name vs field
(version: 0)
Comparing performance of:
instanceof vs constructor.name vs field
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var A = class A { _className = 'A' } var a = new A()
Tests:
instanceof
var b = a instanceof A
constructor.name
var b = a.constructor.name === 'A'
field
var b = a._className === 'A'
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
instanceof
constructor.name
field
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
yesterday
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36
Browser/OS:
Chrome 148 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
instanceof
89155480.0 Ops/sec
constructor.name
30207492.0 Ops/sec
field
91842432.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested, compared, and their pros/cons. **Benchmark Definition** The benchmark definition is a JSON object that contains three test cases: 1. `instanceof` 2. `constructor.name` 3. `field` Each test case represents a different way to check if an instance of a class `A` is valid. **What's being tested?** In this benchmark, we're testing the performance difference between three ways to check if an object `a` is an instance of class `A`, which has a private property `_className` set to `'A'`. The test cases are: 1. `instanceof A`: checks if `a` is an instance of class `A`. 2. `constructor.name === 'A'`: checks the name of the constructor function of `a`. Since `a` is created using the class syntax, its constructor function has the same name as the class. 3. `_className === 'A'`: directly accesses the private property `_className` of class `A`. **Options compared** The benchmark compares the performance difference between these three approaches: 1. Using the `instanceof` operator (`a instanceof A`) 2. Accessing the constructor name using the `constructor.name` property (`a.constructor.name === 'A'`) 3. Directly accessing a private property (`_className === 'A'`) **Pros and Cons** Here's a brief summary of each approach: 1. **Using `instanceof`**: * Pros: Simple, easy to read, and maintain. * Cons: May have performance overhead due to the need for a runtime check. 2. **Accessing `constructor.name`**: * Pros: Fast, as it only requires a single property access. * Cons: May not work correctly if the class has multiple constructors or if the constructor name is changed at runtime. 3. **Directly accessing `_className`**: * Pros: Fast and direct, as it only accesses a private property. * Cons: May break if the private property is renamed, deleted, or modified in an unexpected way. **Library** There's no explicit library mentioned in the benchmark definition or test cases. However, class syntax and private properties are part of the JavaScript language. **Special JS feature or syntax** The `class` keyword and private properties (`_className`) were introduced in ECMAScript 2015 (ES6). The `instanceof` operator has been a part of JavaScript since its inception. Overall, this benchmark is testing the performance trade-offs between three different approaches to check if an object is an instance of a class. The results can help developers understand which approach to use depending on their specific use case and performance requirements.
Related benchmarks:
typeof vs instanceof Function vs call
Check object. typeof vs constructor + null check
Check object. typeof vs constructor
JS instanceof vs in
instanceof vs typeof franco
Comments
Confirm delete:
Do you really want to delete benchmark?