If Statement Performance

Viewed 195

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
}
6 Answers

No, 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 compare 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 comparing 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 completely 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 comparisons. So you should use whichever form is more readable and intuitive in your particular context.

Usually the first one is more clear compared 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 comparisons
Action 2 - 2-3 comparisons
Action 3 - 2-3 comparisons

# in second example
Action 1 - 2 comparisons
Action 2 - 1 comparisons
Action 3 - 2 comparisons

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 comparison is processed, the else block is immediately entered. But the performance difference is very negligible.

Related