switch vs map

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
switch
function getCaffeine(type){
	let caffeine;

	switch(type){
		case 'Coffee':
			caffeine = '95 mg';
			break;
		case 'Redbull':
			caffeine = '147 mg';
			break;
		case 'Tea':
			caffeine = '11 mg';
			break;
		case 'Soda':
			caffeine = '21 mg';
			break;
		default:
			caffeine = 'Not found';
	}
	return caffeine;
}

console.log(getCaffeine('Redbull'));
ready
map
function getCaffeine(type){
	const map = {
		'Coffee': '95 mg',
		'Redbull': '147 mg',
		'Tea': '11 mg',
		'Soda': '21 mg'		
	};
	
	return map[type] ?? 'Not found';
}

console.log(getCaffeine('Redbull'));
ready

Revisions

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