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');
t instanceof VarBaseClass
VarBaseClass.prototype.isPrototypeOf(t)
t instanceof VarAnotherClass
VarAnotherClass.prototype.isPrototypeOf(t)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
success - instanceof | |
success - isPrototypeOf | |
error - instanceof | |
error- isPrototypeOf |
Test name | Executions per second |
---|---|
success - instanceof | 4271940.0 Ops/sec |
success - isPrototypeOf | 4088848.8 Ops/sec |
error - instanceof | 4258877.5 Ops/sec |
error- isPrototypeOf | 4002993.8 Ops/sec |
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:
t instanceof VarBaseClass
: This test checks if an instance of ChildClass
is considered a subclass of BaseClass
. Since ChildClass
extends BaseClass
, this should pass.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.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:
t instanceof VarBaseClass
:VarBaseClass.prototype.isPrototypeOf(t)
:t instanceof VarAnotherClass
and VarAnotherClass.prototype.isPrototypeOf(t)
: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.