If vs switch

Benchmark created on


Setup

const data = [...Array(1000).keys()].map(String)

Test runner

Ready to run.

Testing in
TestOps/sec
If-else
function ifelse(value) {
	if (value === "123") return "is 123"
	else if (value.includes("5")) return "has 5"
	else if (/[02468]^/.test(value)) return "is even"
	else return "something else"
}


data.forEach(ifelse)
ready
Just if
function justif(value) {
	if (value === "123") return "is 123"
	if (value.includes("5")) return "has 5"
	if (/[02468]^/.test(value)) return "is even"
	return "something else"
}


data.forEach(justif)
ready
switch-case
function switchcase(value) {
	switch(true) {
	  case (value === "123"): return "is 123"
	  case (value.includes("5")): return "has 5"
	  case (/[02468]^/.test(value)): return "is even"
	  default: return "something else"
	}
}


data.forEach(switchcase)
ready

Revisions

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