IF vs IF-ELSE vs Switch Case vs Lookup Table - 50 Options (v3)

Revision 3 of this benchmark created on


Setup

function getRandomOption() {
    const options = ["Option 1", "Option 2", "Option 3","Option 4","Option 5","Option 6","Option 7","Option 8","Option 9","Option 10","Option 11","Option 12","Option 13","Option 14","Option 15","Option 16","Option 17","Option 18","Option 19","Option 20","Option 21","Option 22","Option 23","Option 24","Option 25", "unknown"];
    const randomIndex = Math.floor(Math.random() * options.length);
 
    return options[randomIndex];
}

const randomOption = getRandomOption();

Test runner

Ready to run.

Testing in
TestOps/sec
IF-ELSE-IF
if(randomOption === "Option 1") {
	console.log(1);
} else if(randomOption === "Option 2") {
	console.log(2);
} else if(randomOption === "Option 3") {
	console.log(3);
} else if(randomOption === "Option 4") {
	console.log(4);
} else if(randomOption === "Option 5") {
	console.log(5);
} else if(randomOption === "Option 6") {
	console.log(6);
} else if(randomOption === "Option 7") {
	console.log(7);
} else if(randomOption === "Option 8") {
	console.log(8);
} else if(randomOption === "Option 9") {
	console.log(9);
} else if(randomOption === "Option 10") {
	console.log(10);
} else if(randomOption === "Option 11") {
	console.log(11);
} else if(randomOption === "Option 12") {
	console.log(12);
} else if(randomOption === "Option 13") {
	console.log(13);
} else if(randomOption === "Option 14") {
	console.log(14);
} else if(randomOption === "Option 15") {
	console.log(15);
} else if(randomOption === "Option 16") {
	console.log(16);
} else if(randomOption === "Option 17") {
	console.log(17);
} else if(randomOption === "Option 18") {
	console.log(18);
} else if(randomOption === "Option 19") {
	console.log(19);
} else if(randomOption === "Option 20") {
	console.log(20);
} else if(randomOption === "Option 21") {
	console.log(21);
} else if(randomOption === "Option 22") {
	console.log(22);
} else if(randomOption === "Option 23") {
	console.log(23);
} else if(randomOption === "Option 24") {
	console.log(24);
} else if(randomOption === "Option 25") {
	console.log(25);
} else {
	console.log('NONE')
}

ready
SWITCH-CASE
switch(randomOption) {
	case "Option 1": 
		console.log(1);
		break;
	case "Option 2": 
		console.log(2);
		break;
	case "Option 3": 
		console.log(3);
		break;
	case "Option 4": 
		console.log(4);
		break;
	case "Option 5": 
		console.log(5);
		break;
	case "Option 6": 
		console.log(6);
		break;
	case "Option 7": 
		console.log(7);
		break;
	case "Option 8": 
		console.log(8);
		break;
	case "Option 9": 
		console.log(9);
		break;
	case "Option 10": 
		console.log(10);
		break;
	case "Option 11": 
		console.log(11);
		break;
	case "Option 12": 
		console.log(12);
		break;
	case "Option 13": 
		console.log(13);
		break;
	case "Option 14": 
		console.log(14);
		break;
	case "Option 15": 
		console.log(15);
		break;
	case "Option 16": 
		console.log(16);
		break;
	case "Option 17": 
		console.log(17);
		break;
	case "Option 18": 
		console.log(18);
		break;
	case "Option 19": 
		console.log(19);
		break;
	case "Option 20": 
		console.log(20);
		break;
	case "Option 21": 
		console.log(21);
		break;
	case "Option 22": 
		console.log(22);
		break;
	case "Option 23": 
		console.log(23);
		break;
	case "Option 24": 
		console.log(24);
		break;
	case "Option 25": 
		console.log(25);
		break;
	default:
		console.log('NONE')
}

ready
Lookup Table
const results = {
	"Option 1": () => {
		console.log(1);
	},
	"Option 2": () => {
		console.log(2);
	},
	"Option 3": () => {
		console.log(3);
	},
	"Option 4": () => {
		console.log(4);
	},
	"Option 5": () => {
		console.log(5);
	},
	"Option 6": () => {
		console.log(6);
	},
	"Option 7": () => {
		console.log(7);
	},
	"Option 8": () => {
		console.log(8);
	},
	"Option 9": () => {
		console.log(9);
	},
	"Option 10": () => {
		console.log(10);
	},
	"Option 11": () => {
		console.log(11);
	},
	"Option 12": () => {
		console.log(12);
	},
	"Option 13": () => {
		console.log(13);
	},
	"Option 14": () => {
		console.log(14);
	},
	"Option 15": () => {
		console.log(15);
	},
	"Option 16": () => {
		console.log(16);
	},
	"Option 17": () => {
		console.log(17);
	},
	"Option 18": () => {
		console.log(18);
	},
	"Option 19": () => {
		console.log(19);
	},
	"Option 20": () => {
		console.log(20);
	},
	"Option 21": () => {
		console.log(21);
	},
	"Option 22": () => {
		console.log(22);
	},
	"Option 23": () => {
		console.log(23);
	},
	"Option 24": () => {
		console.log(24);
	},
	"Option 25": () => {
		console.log(25);
	},
}

results[randomOption] ? results[randomOption]() : console.log('NONE');

ready
Bonus: Switch Pattern Matching
switch(true) {
	case randomOption === "Option 1": 
		console.log(1);
		break;
	case randomOption ===  "Option 2": 
		console.log(2);
		break;
	case randomOption === "Option 3": 
		console.log(3);
		break;
	case randomOption ===  "Option 4": 
		console.log(4);
		break;
	case randomOption === "Option 5":
		console.log(5);
		break;
	case randomOption === "Option 6":
		console.log(6);
		break;
	case randomOption === "Option 7":
		console.log(7);
		break;
	case randomOption === "Option 8":
		console.log(8);
		break;
	case randomOption === "Option 9":
		console.log(9);
		break;
	case randomOption === "Option 10":
		console.log(10);
		break;
	case randomOption === "Option 11":
		console.log(11);
		break;
	case randomOption === "Option 12":
		console.log(12);
		break;
	case randomOption === "Option 13":
		console.log(13);
		break;
	case randomOption === "Option 14":
		console.log(14);
		break;
	case randomOption === "Option 15":
		console.log(15);
		break;
	case randomOption === "Option 16":
		console.log(16);
		break;
	case randomOption === "Option 17":
		console.log(17);
		break;
	case randomOption === "Option 18":
		console.log(18);
		break;
	case randomOption === "Option 19":
		console.log(19);
		break;
	case randomOption === "Option 20":
		console.log(20);
		break;
	case randomOption === "Option 21":
		console.log(21);
		break;
	case randomOption === "Option 22":
		console.log(22);
		break;
	case randomOption === "Option 23":
		console.log(23);
		break;
	case randomOption === "Option 24":
		console.log(24);
		break;
	case randomOption === "Option 25":
		console.log(25);
		break;
	default:
		console.log('NONE')
}
ready

Revisions

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