instanceof vs constructor.name

Benchmark created on


Setup

class TestA {
	
}


class TestB {
	
}

var tests = []
for(var i=0;i<100;i++) {
	var test = Math.random() > 0.5 ? new TestA() : new TestB()
	tests.push(test)
}

var a = 0;
var b = 0;

Test runner

Ready to run.

Testing in
TestOps/sec
instanceof
for(var test of tests) {
	if(test instanceof TestA) {
		a++
	} else if(test instanceof TestB) {
		b++
	}
}
ready
constructor.name
for(var test of tests) {
	if(test.constructor.name == "TestA") {
		a++
	} else if(test.constructor.name == "TestB") {
		b++
	}
}
ready

Revisions

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