Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Int switch vs str switch vs int if-else vs str if-else vs int object-key vs str object-key
(version: 0)
Comparing performance of:
Int switch vs str switch vs int if-else vs str if-else vs int object-key vs str object-key
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var intKey = [0,1,2,3,4,5,6,7,8,9,10] var intObj = {0:'zero', 1:'one', 2:'two', 3:'three', 4:'four', 5:'five', 6:'six', 7:'seven', 8:'eight', 9:'nine', 10:'ten'} var strKey = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'] var strObj = {'0':'zero', '1':'one', '2':'two', '3':'three', '4':'four', '5':'five', '6':'six', '7':'seven', '8':'eight', '9':'nine', '10':'ten'}
Tests:
Int switch
res=[] for(let i = 0; i<=10; i++){ switch(intKey[i]){ case 0: res.push(intObj[0]) break case 1: res.push(intObj[1]) break case 2: res.push(intObj[2]) break case 3: res.push(intObj[3]) break case 4: res.push(intObj[4]) break case 5: res.push(intObj[5]) break case 6: res.push(intObj[6]) break case 7: res.push(intObj[7]) break case 8: res.push(intObj[8]) break case 9: res.push(intObj[9]) break case 10: res.push(intObj[10]) break } }
str switch
res=[] for(let i = 0; i<=10; i++){ switch(strKey[i]){ case '0': res.push(strObj['0']) break case '1': res.push(strObj['1']) break case '2': res.push(strObj['2']) break case '3': res.push(strObj['3']) break case '4': res.push(strObj['4']) break case '5': res.push(strObj['5']) break case '6': res.push(strObj['6']) break case '7': res.push(strObj['7']) break case '8': res.push(strObj['8']) break case '9': res.push(strObj['9']) break case '10': res.push(strObj['10']) break } }
int if-else
res=[] for(let i = 0; i<=10; i++){ console.log(intKey[i]) if(intKey[i]==0){ res.push(intObj[0]) }else if(intKey[i]==1){ res.push(intObj[1]) }else if(intKey[i]==2){ res.push(intObj[2]) }else if(intKey[i]==3){ res.push(intObj[3]) }else if(intKey[i]==4){ res.push(intObj[4]) }else if(intKey[i]==5){ res.push(intObj[5]) }else if(intKey[i]==6){ res.push(intObj[6]) }else if(intKey[i]==7){ res.push(intObj[7]) }else if(intKey[i]==8){ res.push(intObj[8]) }else if(intKey[i]==9){ res.push(intObj[9]) }else if(intKey[i]==10){ res.push(intObj[10]) } }
str if-else
res=[] for(let i = 0; i<=10; i++){ if(strKey[i]==0){ res.push(strObj[0]) }else if(strKey[i]==1){ res.push(strObj[1]) }else if(strKey[i]==2){ res.push(strObj[2]) }else if(strKey[i]==3){ res.push(strObj[3]) }else if(strKey[i]==4){ res.push(strObj[4]) }else if(strKey[i]==5){ res.push(strObj[5]) }else if(strKey[i]==6){ res.push(strObj[6]) }else if(strKey[i]==7){ res.push(strObj[7]) }else if(strKey[i]==8){ res.push(strObj[8]) }else if(strKey[i]==9){ res.push(strObj[9]) }else if(strKey[i]==10){ res.push(strObj[10]) } }
int object-key
res=[] for(let i = 0; i<=10; i++){ res.push(intObj[intKey[i]]) }
str object-key
res=[] for(let i = 0; i<=10; i++){ res.push(strObj[strKey[i]]) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
Int switch
str switch
int if-else
str if-else
int object-key
str object-key
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Based on the provided benchmark results, I'll analyze and summarize the performance of each test. **Int switch**: The highest execution rate is 201687.140625 per second. **int object-key**: The second-highest execution rate is 199034.21875 per second. **str object-key**: A close third is 198974.1875 per second. **str switch**: This test has the lowest execution rate at 85117.1015625 per second, indicating a significant performance difference compared to other tests. **str if-else**: Another low-performance test with an execution rate of 4070.7373046875 per second. These results suggest that: * Using switches (int and str) is faster than using object keys or nested switch statements. * The order of operations within the benchmark tests can significantly impact performance. * Introducing conditional statements like if-else further reduces performance compared to simpler statement execution. Please let me know if you'd like me to elaborate on any specific aspect of these findings!
Related benchmarks:
Spread vs object assign
object to array with lodash
Int switch vs str switch vs int if-else vs str if-else vs int object-key vs str object-key v2
Object.keys vs for-in
Comments
Confirm delete:
Do you really want to delete benchmark?