class NewError extends Error {
constructor() {
super()
this.customProp = 'blah'
}
customProp
}
const testErr = new NewError()
testErr instanceof NewError
class NewError extends Error {
constructor() {
super()
this.customProp = 'blah'
}
customProp
}
const testErr = new NewError()
!!testErr && typeof testErr === 'object' && 'customProp' in testErr
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
instanceof | |
prop test |
Test name | Executions per second |
---|---|
instanceof | 69844.6 Ops/sec |
prop test | 71144.2 Ops/sec |
Overview of the Benchmark
The provided benchmark is designed to measure the performance of JavaScript engines in detecting whether a variable var
is an instance of a class. The test cases are simple, yet revealing of how different JavaScript engines approach type checking and object property access.
Benchmark Definition
The benchmark definition represents a script that defines a custom error class NewError
with a custom property customProp
. A new instance of the NewError
class is created and used to create two test cases:
instanceof
: Verifies whether the instance of NewError
is itself an instance of the NewError
class using the instanceof
operator.prop test
: Checks if the instance has a property named customProp
using the in
operator and checks if it's a string.Options Compared
The two options being compared are:
instanceof
operator to check for class inheritancein
operator to access object propertiesPros and Cons of Each Approach
instanceof
instanceof
operator is a built-in operator that checks if an instance is an instance of a specific constructor function or class.in
instanceof
for certain scenarios.Library Used
None, as this benchmark does not rely on any external libraries. The test code is self-contained within the JSON definitions.
Special JS Feature or Syntax
The instanceof
operator and the in
operator are built-in JavaScript operators that do not require special knowledge of features or syntax beyond basic JavaScript syntax.
Other Alternatives
In general, other alternatives to these approaches might involve:
Keep in mind that these alternatives may have varying levels of feasibility, complexity, and performance impact compared to the original benchmark options.