If-then-else vs. switch check

Benchmark created on


Setup

const array = [];
let result;

function isString (value) { return typeof value === "string" || value instanceof String };
function isNumber (value) { return Number.isFinite(value) };

Test runner

Ready to run.

Testing in
TestOps/sec
if-then-else
if (isString(array)) {
	result = "string"
} else if (isNumber(array)) {
	result = "number"
} else {
	result = "array"
}
return result;
ready
switch
switch (true) {
	case isString(array):
		result = "string";
		break;
	case isNumber(array):
		result = "number";
		break;
	default:
		result = "array";
}
ready

Revisions

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