instanceof vs prop

Benchmark created on


Setup

const s = Symbol('a')
class A {
  constructor(x) {
    this.x = x;
  }
}
class B {
  constructor(x) {
  	this.x = x;
    this.y = s;
  }
}
function isA(x) {
  return x instanceof A;
}
function isB(x) {
  return x.y === s;
}
function isC(x) {
  return { y: s }
}
function isD(x) {
  return !!x[s];
}

Test runner

Ready to run.

Testing in
TestOps/sec
A
isA(new A(42))
ready
B
isB(new B(42))
ready
C
isC({ x: 42, y: s })
ready
D
isD({ x: 42, [s]: true })
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.