# If..else If || Switch || Eval

## Recommended Posts

Hello all,I came across something today, and have been wondering which of the following methods executes the fastest. I know it might be obvious here, but Im wondering what some of your opinions are.Method 1 - if..else if:

`function playSound(what){	if(what===0){		posSound0.play();	}	else if(what===1){		posSound1.play();	}	else if(what===2){		posSound2.play();	}	else if(what===3){		posSound3.play();	}	else if(what===4){		posSound4.play();	}	else if(what===5){		posSound5.play();	}	else if(what===6){		posSound6.play();	}	else if(what===7){		posSound7.play();	}	}`

Method 2 - switch:

`function playSound(what){	switch(what){		case 0:		posSound0.play();		break;		case 1:		posSound1.play();		break;		case 2:		posSound2.play();		break;		case 3:		posSound3.play();		break;		case 4:		posSound4.play();		break;		case 5:		posSound5.play();		break;		case 6:		posSound6.play();		break;			}}`

Method 3 - eval:

`function playSound(what){  eval("posSound"+what+".play()");}`

##### Share on other sites

What's not obvious is that setting up unique variables for posSound0, posSound1, and so on is the thing that creates your dilemma. This type of situation is EXACTLY what arrays are created for. What you really want is something initialized like this:

`posSound = new Array();posSound[0] = something;posSound[1] = somethingelse;// and so on`

and then you'll just do this:

`function playSound(what){	posSound[what].play();}`

You were very close with your numbering scheme, and the eval idea came closest to a robust (infinitely expandable) technique, but was actually the slowest, because evaluating strings into code is just plain slow. What I've done is taken that idea to its logical conclusion. Having the array allows us to dump the eval statement, and also to dump the if/switch control structures, which likewise introduce a slowness that can be avoided.Now, when you get right down to it, since the playSound() function only has one statement, you really don't even need it. Just call the play() method of the appropriate posSound member, and you're good. That too will increase speed, and also reduce the size of your script.

##### Share on other sites

What's not obvious is that setting up unique variables for posSound0, posSound1, and so on is the thing that creates your dilemma. This type of situation is EXACTLY what arrays are created for. What you really want is something initialized like this:
`posSound = new Array(); posSound[0] = something; posSound[1] = somethingelse; // and so on`

and then you'll just do this:

`function playSound(what){	 posSound[what].play(); }`

You were very close with your numbering scheme, and the eval idea came closest to a robust (infinitely expandable) technique, but was actually the slowest, because evaluating strings into code is just plain slow. What I've done is taken that idea to its logical conclusion. Having the array allows us to dump the eval statement, and also to dump the if/switch control structures, which likewise introduce a slowness that can be avoided.Now, when you get right down to it, since the playSound() function only has one statement, you really don't even need it. Just call the play() method of the appropriate posSound member, and you're good. That too will increase speed, and also reduce the size of your script.

Thanks for the thorough response!I think your last suggestion is best, since I dont need a function for it after all. And since that function would have been executed indeterminably based on user interaction, I think the function-less solution for this is best, unless in the future I require more complex if/else statments. Thanks again!