The if statement
Discussions usually begin surrounding complex if statements such as this:
if (value == 0){
return result0;
} else if (value == 1){
return result1;
} else if (value == 2){
return result2;
} else if (value == 3){
return result3;
} else if (value == 4){
return result4;
} else if (value == 5){
return result5;
} else if (value == 6){
return result6;
} else if (value == 7){
return result7;
} else if (value == 8){
return result8;
} else if (value == 9){
return result9;
} else {
return result10;
}
Typically, this type of construct is frowned upon. The major problem is that the deeper into the statement the execution flows, the more conditions have to be evaluated. It will take longer to complete the execution when value is 9 than if value is 0 because every other condition must be evaluated beforehand. As the overall number of conditions increases, so does the performance hit for going deep into the conditions. While having a large number of if conditions isn’t advisable, there are steps you can take to increase the overall performance.
The first step is to arrange the conditions in decreasing order of frequency. Since exiting after the first condition is the fastest operation, you want to make sure that happens as often as possible. Suppose the most common case in the previous example is that value will equal 5 and the second most common is that value will equal 9. In that case, you know five conditions will be evaluated before getting to the most common case and nine before getting to the second most common case; this is incredibly inefficient. Even though the increasing numeric order of the conditions makes it easier to read, it should actually be rewritten as follows:
if (value == 5){
return result5;
} else if (value == 9){
return result9;
} else if (value == 0){
return result0;
} else if (value == 1){
return result1;
} else if (value == 2){
return result2;
} else if (value == 3){
return result3;
} else if (value == 4){
return result4;
} else if (value == 6){
return result6;
} else if (value == 7){
return result7;
} else if (value == 8){
return result8;
} else {
return result10;
}
Now the two most common conditions appear at the top of the if statement, ensuring optimal performance for these cases.
Another way to optimize if statements is to organize the conditions into a series of branches, following a binary search algorithm to find the valid condition. This is advisable in the case where a large number of conditions are possible and no one or two will occur with a high enough rate to simply order according to frequency. The goal is to minimize the number of conditions to be evaluated for as many of the conditions as possible. If all of the conditions for value in the example will occur with the same relative frequency, the if statements can be rewritten as follows:
if (value < 6){
if (value < 3){
if (value == 0){
return result0;
} else if (value == 1){
return result1;
} else {
return result2;
}
} else {
if (value == 3){
return result3;
} else if (value == 4){
return result4;
} else {
return result5;
}
}
} else {
if (value < 8){
if (value == 6){
return result6;
} else {
return result7;
}
} else {
if (value == 8){
return result8;
} else if (value == 9){
return result9;
} else {
return result10;
}
}
}
This code ensures that there will never be any more than four conditions evaluated. Instead of evaluating each condition to find the right value, the conditions are separated first into a series of ranges before identifying the actual value. The overall performance of this example is improved because the cases where eight and nine conditions need to be evaluated have been removed. The maximum number of condition evaluations is now four, creating an average savings of about 30% off the execution time of the previous version. Keep in mind, also, that an else statement has no condition to evaluate. However, the problem remains that each additional condition ends up taking more time to execute, affecting not only the performance but also the maintainability of this code. This is where the switch statement comes in.
The switch statement
The switch statement simplifies both the appearance and the performance of multiple conditions. You can rewrite the previous example using a switch statement as follows:
switch(value){
case 0:
return result0;
case 1:
return result1;
case 2:
return result2;
case 3:
return result3;
case 4:
return result4;
case 5:
return result5;
case 6:
return result6;
case 7:
return result7;
case 8:
return result8;
case 9:
return result9;
default:
return result10;
}
This code clearly indicates the conditions as well as the return values in an arguably more readable form. The switch statement has the added benefit of allowing fall-through conditions, which allow you to specify the same result for a number of different values without creating complex nested conditions. The switch statement is often cited in other programming languages as the hands-down better option for evaluating multiple conditions. This isn’t because of the nature of the switch statement, but rather because of how compilers are able to optimize switch statements for faster evaluation.
Another option: Array lookup
There are more than two solutions for dealing with conditionals in JavaScript. Alongside the if statement and the switch statement is a third approach: looking up values in arrays
for(String s: arr){
if(s.equals(targetValue))
return true;
}
return false;
Although array lookup times also increase the deeper into the array you go, the incremental increase is so small that it is irrelevant relative to the increases in each condition evaluation for if and switch statements. This makes array lookup ideal whenever there are a large number of conditions to be met, and the conditions can be represented by discrete values such as numbers or strings
The fastest conditionals
The three techniques presented here—the if statement, the switch statement, and array lookup—each have their uses in optimizing code execution:
•Use the if statement when:
- There are no more than two discrete values for which to test.
- There are a large number of values that can be easily separated into ranges.
•Use the switch statement when:
- There are more than two but fewer than 10 discrete values for which
to test.
- There are no ranges for conditions because the values are nonlinear.
•Use array lookup when:
- There are more than 10 values for which to test.
- The results of the conditions are single values rather than a number
of actions to be taken.
str1.equals(str2)is the most efficient way to do what you need without writing compilcated/hard to understand code like you posted.nis 10 andmis length of string that is being searched, That's the worst case. BTWequals()will return on the first unmatched character. So that's not an issue. If you ask me, then ~10 strings is too less a number , even to do micro-benchmarking. You shouldn't worry about performance in such trivial cases.