instanceof if then else or switch case on constructore

Benchmark created on


Description

what is faster, instanceof decision tree or switch case on constructor?

Setup

var objects = [[], /a/ig, new Date(), 1, 1n];

Test runner

Ready to run.

Testing in
TestOps/sec
instanceof
objects.forEach(function(obj){
	if (obj instanceof Array) {
		return 1;
	}
	else
	if (obj instanceof RegExp) {
		return 2;
	}
	else
	if (obj instanceof Date) {
		return 3;
	}
	else
	if (obj instanceof Number) {
		return 4;
	}
	else
	if (obj instanceof BigInt) {
		return 5;
	}
})
ready
switch on constructor
objects.forEach(function(obj){
	switch (obj.constructor){
	  	case Array:
			return 1;
		case RegExp:
			return 2;
		case Date:
			return 3;
		case Number:
			return 4;
		case BigInt:
			return 5;
	}
})
ready

Revisions

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