string vs error cmp

Benchmark created on


Setup

class NameChangedError extends Error {
  constructor() {
    super()
    this.name = 'SomeTypeOfError'
  }
}

class InstanceOfError extends Error {}

const ERROR_MSG_1 = "Some kind of error"

let x = 0

Teardown

console.log(x)

Test runner

Ready to run.

Testing in
TestOps/sec
message comparison
try {
	throw new Error(ERROR_MSG_1)
} catch (e) {
	if (e.message === ERROR_MSG_1) {
		x += 1;
	}
}
ready
name comparison
try {
	throw new NameChangedError()
} catch (e) {
	if (e.name === 'SomeTypeOfError') {
		x += 1;
	}
}
ready
instanceof
try {
	throw new InstanceOfError()
} catch (e) {
	if (e instanceof InstanceOfError) {
		x += 1;
	}
}
ready

Revisions

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