Long Ternary vs Swtich vs If...Else

Benchmark created by EtnasSoft on


Test runner

Ready to run.

Testing in
TestOps/sec
Ternary
var a = 11;
var numberLiteral = a == 5 ? 'Five' : a == 7 ? 'Seven' : a == 11 ? 'Eleven' : a == 15 ? 'Fifteen' : 'Other Number';
ready
If...Else
var a = 11,
    numberLiteral;

if (a == 5) {
 numberLiteral = 'Five';
} else if (a == 7) {
 numberLiteral = 'Seven';
} else if (a == 11) {
 numberLiteral = 'Eleven';
} else if (a == 15) {
 numberLiteral = 'Fifteen';
} else {
 numberLiteral = 'Other Number';
}
ready
Switch
var a = 11,
    numberLiteral;

switch (a) {
case 5:
 numberLiteral = 'Five';
 break;
case 7:
 numberLiteral = 'Seven';
 break;
case 11:
 numberLiteral = 'Eleven';
 break;
case 15:
 numberLiteral = 'Fifteen';
 break;
default:
 numberLiteral = 'Other Number';
}
ready

Revisions

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