I thought of a hypothetical performance question regarding conditions within loops that would apply to most languages. Suppose you have a simple loop (I’ll use JavaScript for the example since it’s a very common language):
var favoriteJuices = new Array();
favoriteJuices["Mike"] = "Apple";
favoriteJuices["Fred"] = "Orange";
favoriteJuices["James"] = "Orange";
favoriteJuices["Samantha"] = "Grapefruit";
favoriteJuices["Jane"] = "Apple";
favoriteJuices["Elizabeth"] = "Apple";
for(var i in favoriteJuices){
document.writeln(i+" likes "+favoriteJuices[i]);
}
Now, suppose you didn’t want to display anyone who likes apple juice. Would it be more efficient to write:
for(var i in favoriteJuices){
if(i!="Apple"){
document.writeln(i+" likes "+favoriteJuices[i]);
}
}
or:
for(var i in favoriteJuices){
if(i=="Apple") continue;
document.writeln(i+" likes "+favoriteJuices[i]);
}
?
If there is even much of a difference, to what magnitude would performance be affected?