Created
May 21, 2021 20:03
-
-
Save arielivandiaz/2889ea9423cc66a8827d141b59a681ea to your computer and use it in GitHub Desktop.
Switch case vs Const Object Performance - Clean code with better performance.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Switch case vs Const Object Performance | |
//Const Object is 20%-50% faster | |
//Comment the lines of code for "counter [x] ++;" and check the difference | |
const NAMES = [ | |
'Zarya', | |
'Inna', | |
'Antoniya', | |
'Filippa', | |
'Irina', | |
'Sevastian', | |
'Arisha', | |
'Dmitriy', | |
'Vlad', | |
'Mariya', | |
'Svetlana', | |
'Nastasya', | |
'Yuliana', | |
'Viktoriya']; | |
const SET_DEFAULT_NAME = 'Elena'; | |
const SET_NAME = { | |
Zarya: 'Zarya', | |
Inna: 'Inna', | |
Antoniya: 'Antoniya', | |
Filippa: 'Filippa', | |
Irina: 'Irina', | |
Sevastian: 'Sevastian', | |
Arisha: 'Arisha', | |
Dmitriy: 'Dmitriy', | |
Vlad: 'Vlad', | |
Mariya: 'Mariya', | |
Svetlana: 'Svetlana', | |
Nastasya: 'Nastasya', | |
Yuliana: 'Yuliana', | |
Viktoriya: 'Viktoriya' | |
}; | |
var counter = { | |
Zarya: 0, | |
Inna: 0, | |
Antoniya: 0, | |
Filippa: 0, | |
Irina: 0, | |
Sevastian: 0, | |
Arisha: 0, | |
Dmitriy: 0, | |
Vlad: 0, | |
Mariya: 0, | |
Svetlana: 0, | |
Nastasya: 0, | |
Yuliana: 0, | |
Viktoriya: 0, | |
Elena: 0 | |
}; | |
var randomSequence = []; | |
for (var i = 0; i < 10000000; i++) | |
randomSequence.push(Math.floor(Math.random() * (NAMES.length + 1))); | |
console.time("switch"); | |
for (var j = 0; j < randomSequence.length; j++) { | |
var x = ''; | |
switch (NAMES[randomSequence[j]]) { | |
case 'Zarya': | |
x = NAMES[randomSequence[j]]; | |
break; | |
case 'Inna': | |
x = NAMES[randomSequence[j]]; | |
break; | |
case 'Antoniya': | |
x = NAMES[randomSequence[j]]; | |
break; | |
case 'Filippa': | |
x = NAMES[randomSequence[j]]; | |
break; | |
case 'Irina': | |
x = NAMES[randomSequence[j]]; | |
break; | |
case 'Sevastian': | |
x = NAMES[randomSequence[j]]; | |
break; | |
case 'Arisha': | |
x = NAMES[randomSequence[j]]; | |
break; | |
case 'Dmitriy': | |
x = NAMES[randomSequence[j]]; | |
break; | |
case 'Vlad': | |
x = NAMES[randomSequence[j]]; | |
break; | |
case 'Mariya': | |
x = NAMES[randomSequence[j]]; | |
break; | |
case 'Svetlana': | |
x = NAMES[randomSequence[j]]; | |
break; | |
case 'Nastasya': | |
x = NAMES[randomSequence[j]]; | |
break; | |
case 'Yuliana': | |
x = NAMES[randomSequence[j]]; | |
break; | |
case 'Viktoriya': | |
x = NAMES[randomSequence[j]]; | |
break; | |
default: | |
x = SET_DEFAULT_NAME; | |
} | |
counter[x]++; | |
} | |
console.timeEnd("switch"); | |
counter = { | |
Zarya: 0, | |
Inna: 0, | |
Antoniya: 0, | |
Filippa: 0, | |
Irina: 0, | |
Sevastian: 0, | |
Arisha: 0, | |
Dmitriy: 0, | |
Vlad: 0, | |
Mariya: 0, | |
Svetlana: 0, | |
Nastasya: 0, | |
Yuliana: 0, | |
Viktoriya: 0, | |
Elena: 0 | |
}; | |
console.time("constObject"); | |
for (var j = 0; j < randomSequence.length; j++) { | |
var x = ''; | |
x = SET_NAME[NAMES[randomSequence[j]]] || SET_DEFAULT_NAME; | |
counter[x]++; | |
} | |
console.timeEnd("constObject"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment