instanceof alternatives

Benchmark created on


Setup

class Foo {
  isFoo = true
}

const foo = new Foo()

function isInstanceOf(obj, type) {
  const propertyToCheck = `is${type}`
  return (
    typeof obj === 'object' &&
    obj !== null &&
    !!obj[propertyToCheck]
  )
}

const is = new Proxy(
  {},
  {
    get(_, prop) {
      return (obj) => isInstanceOf(obj, prop)
    }
  }
)

Test runner

Ready to run.

Testing in
TestOps/sec
instanceof
foo instanceof Foo
ready
isInstanceOf
isInstanceOf(foo, 'Foo')
ready
is
is.Foo(foo)
ready

Revisions

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