Given the following code snippet:
if (condition1 && condition2) {
// ACTION 1
} else if (!condition1) {
// ACTION 2
} else {
// ACTION 3
}
Does it make a difference in performance if I restructure it this way:
if (condition1) {
if (condtion2) {
// ACTION 1
} else {
// ACTION 3
}
} else {
// ACTION 2
}
Given the following code snippet:
if (condition1 && condition2) {
// ACTION 1
} else if (!condition1) {
// ACTION 2
} else {
// ACTION 3
}
Does it make a difference in performance if I restructure it this way:
if (condition1) {
if (condtion2) {
// ACTION 1
} else {
// ACTION 3
}
} else {
// ACTION 2
}
Share
Improve this question
edited Oct 7, 2019 at 18:04
FZs
18.7k13 gold badges46 silver badges56 bronze badges
asked Oct 7, 2019 at 18:02
SammySammy
3,7379 gold badges60 silver badges103 bronze badges
2
- I don't think it is really significant difference looking only to the if statements. But looking only for the ifs, the worst case in the 1st is 4 parisons (when Action 3), and in the 2nd, the worst case is 2 parisons (ACTION 1 or 3). Maybe a thuth table can gave you a most interesting result – Luiz Fernando da Silva Commented Oct 7, 2019 at 18:11
-
1
The readability and maintainability of not having to repeat
condition1
in your second snippet are much much more important than the negligible performance improvements. – Bergi Commented Oct 7, 2019 at 18:16
6 Answers
Reset to default 5No, not unless condition1
contains a heavy method call of some sort that would be repeated otherwise. And if it is it would be better to call it first and just pare the results just to keep it easier to read.
let condition1Result = condition1();
if (condition1Result && condition2) {
// ACTION 1
} else if (!condition1Result) {
// ACTION 2
} else {
// ACTION 3
}
Just paring two values has a negligible performance hit in almost all scenarios so focusing on readability of the code of micro-optimization is always a good idea.
In the first case, if condition1
is false
, then there will be a short-circuit operation which basically means that you would not need to check condition2
and pletely skip that block altogether.
Whereas in the second case, you can skip two blocks if condition1
is false
.
As far as the performance is considered, it won't have a significant difference since they are just parisons. So you should use whichever form is more readable and intuitive in your particular context.
Usually the first one is more clear pared to if..else nested blocks and is mostly preferred.
Also, performance is different as condition1 && condition
results in short-circuit operations. So, if first condition is false, it won't go ahead and check second condition.
It depends on what condition1 and condition2 are. I can of a scenario where it does. For example, if both conditions are functions that query the database. Condition1 returns true, then condition2 returns false but changes values in the database, that will cause condition1 to return false on the next check.
This is why in the first code, condition1 will go twice to the DB, which is slower performance.
If both conditions are just variables, I think there shouldn't be any difference
I wouldn't worry about if statements like that for performance improvements. They are very similar. What you can do is to see which case is the most likely to happen and put this condition in front of others.
But you can also count number of check operations for each case:
# in first example
Action 1 - 2 parisons
Action 2 - 2-3 parisons
Action 3 - 2-3 parisons
# in second example
Action 1 - 2 parisons
Action 2 - 1 parisons
Action 3 - 2 parisons
From this second example has less conditions to check and should be slightly faster. But this is very small performance improvement.
It depends on condition1
. If condition1
is false then the given statement
if (condition1) {
if (condition2) {
// ACTION 1
} else {
// ACTION 3
}
} else {
// ACTION 2
}
is faster than
if (condition1 && condition2) {
// ACTION 1
} else if (!condition1) {
// ACTION 2
} else {
// ACTION 3
}
because no second parison is processed, the else
block is immediately entered.
But the performance difference is very negligible.