Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Picon dev - insatnce of vs isPrototyOf
(version: 0)
Comparing performance of:
success - instanceof vs success - isPrototypeOf vs error - instanceof vs error- isPrototypeOf
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
class BaseClass { constructor(val) { this._bVal = val; } get bVal() { return this._bVal; } set bVal(value) { this._bVal = value; } } class ParentClass extends BaseClass { constructor(val) { super(val) this._pVal = val + '_parent'; } get pVal() { return this._pVal; } set pVal(value) { this._pVal = value; } } class ChildClass extends BaseClass { constructor(val) { super(val) this._cVal = val + '_child'; } get cVal() { return this._cVal; } set cVal(value) { this._cVal = value; } } class AnotherClass { constructor(val) { this._bVal = val; } get bVal() { return this._bVal; } set bVal(value) { this._bVal = value; } } var VarBaseClass = BaseClass; var VarAnotherClass = AnotherClass; var t = new ChildClass('test');
Tests:
success - instanceof
t instanceof VarBaseClass
success - isPrototypeOf
VarBaseClass.prototype.isPrototypeOf(t)
error - instanceof
t instanceof VarAnotherClass
error- isPrototypeOf
VarAnotherClass.prototype.isPrototypeOf(t)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
success - instanceof
success - isPrototypeOf
error - instanceof
error- isPrototypeOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Definition** The benchmark defines three classes: `BaseClass`, `ParentClass`, and `ChildClass`. The main difference between these classes is how they extend each other: * `BaseClass` has a constructor that takes a value, and two getter/setter methods for accessing a property `_bVal`. * `ParentClass` extends `BaseClass` and adds another constructor that takes a value, as well as two getter/setter methods for accessing a new property `_pVal`. The new constructor calls the parent class's constructor using the `super()` method. * `ChildClass` also extends `BaseClass`, but adds its own constructor with a different initialization logic. It then defines two getter/setter methods for accessing a property `_cVal`. **Options being compared** There are three test cases that compare different approaches: 1. **`t instanceof VarBaseClass`**: This test checks if an instance of `ChildClass` is considered a subclass of `BaseClass`. Since `ChildClass` extends `BaseClass`, this should pass. 2. **`VarBaseClass.prototype.isPrototypeOf(t)`**: This test uses the `prototype` property to check if `t` is an instance of `BaseClass`. The idea behind this test is that if `t` has a prototype chain that includes `BaseClass`, then `isPrototypeOf()` will return true. 3. **`t instanceof VarAnotherClass`** and **`VarAnotherClass.prototype.isPrototypeOf(t)`**: These two tests check similar scenarios, but with a twist: `VarAnotherClass` is not related to the classes defined in the benchmark definition. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: 1. **`t instanceof VarBaseClass`**: * Pros: Simple and straightforward. * Cons: May not work if the inheritance hierarchy is complex or if there are multiple levels of inheritance. 2. **`VarBaseClass.prototype.isPrototypeOf(t)`**: * Pros: Can handle more complex inheritance hierarchies, but may be slower due to the overhead of prototype checking. * Cons: May return false positives if the prototype chain includes unrelated objects. 3. **`t instanceof VarAnotherClass`** and **`VarAnotherClass.prototype.isPrototypeOf(t)`**: * Pros: Can handle more complex inheritance hierarchies, but are less relevant to the benchmark definition. * Cons: Are unnecessary and may introduce noise in the benchmark results. **Library and features** The benchmark uses no external libraries or features beyond the standard JavaScript language. No special JS features or syntax are used in this benchmark. **Other alternatives** There are other ways to check inheritance relationships, such as using the `instanceof` operator with a more complex constructor or using a library like Lodash. However, these alternatives may not be relevant to the specific use case of this benchmark. The benchmark provides useful insights into how the browser and JavaScript engine handle inheritance relationships. The results can help identify performance issues or bugs in the browser's implementation.
Related benchmarks:
Perfomance: Switch vs Polymorphism
Class comp
member vs proto member
ES6 Class vs Prototype vs Object Literal v2 with Extends
Comments
Confirm delete:
Do you really want to delete benchmark?