instanceof

Benchmark created on


Setup

class Animal {
	isDog() {
		return false;
	}
	isCat() {
		return false;
	}
}
class Dog extends Animal {
	isDog() {
		return true;
	}
}
class Cat extends Animal {
	isCat() {
		return true;
	}
}
const animals = [];
for (let i = 0; i < 1000; ++i) {
	const animal = Math.random() < 0.5 ? new Dog() : new Cat();
	animals.push(animal);
}

Test runner

Ready to run.

Testing in
TestOps/sec
instanceof
let count = 0;
for(let i = 0; i < animals.length; ++i) {
	if (animals[i]	instanceof Dog) {
		count += 1;
	}
}
console.log(count);
ready
methid
let count = 0;
for(let i = 0; i < animals.length; ++i) {
	if (animals[i].isDog()) {
		count += 1;
	}
}
console.log(count);
ready

Revisions

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