if vs switch (v2)

Revision 2 of this benchmark created on


Description

You watched one YandereDev code criticism video and now you think you're an expert on optimization. Of course a switch statement is so much faster than an if statement!

Test runner

Ready to run.

Testing in
TestOps/sec
10 chained if statements
let randInt = Math.floor(Math.random() * 10)

if (randInt == 0)
	return 0;
else if (randInt == 1) 
	return 1;
else if (randInt == 2)
	return 2;
else if (randInt == 3)
	return 3;
else if (randInt == 4)
	return 4;
else if (randInt == 5)
	return 5;
else if (randInt == 6)
	return 6;
else if (randInt == 7)
	return 7;
else if (randInt == 8)
	return 8;
else if (randInt == 9)
	return 9;
ready
10 case switch statement
let randInt = Math.floor(Math.random() * 10)


switch(randInt) {
	case 0:
		return 0;
	case 1:
		return 1;
	case 2:
		return 2;
	case 3:
		return 3;
	case 4:
		return 4;
	case 5:
		return 5;
	case 6:
		return 6;
	case 7:
		return 7;
	case 8:
		return 8;
	case 9:
		return 9;
}
ready

Revisions

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