switch vs if-else branching control structure in Node.JS -


which 1 use when there large number of branching flow in node.js program.

switch

switch(n) { case 1:   execute code block 1   break; case 2:   execute code block 2   break; default:   code executed if n different case 1 , 2 } 

or
if-else

if (condition1)   {     execute code block 1   } else if(condition2)   {     execute code block 2   }  else   {      code executed if n different condition1 , condition2   }  

for few items, difference small. if have many items should use switch. give better performance if-else.

if switch contains more 5 items, it's implemented using lookup table or hash list. means items same access time, compared list of if-else last item takes more time reach has evaluate every previous condition first..


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -