0

I've got a for-loop in JavaScript, iterating over the variable i. In each iteration step, a list of if-conditions is checked. For each i, only one of these conditions can be true (or none of them) and every condition is true for exactly one i. A very simple example would be:

for (i = 1; i <= 10; i++)
{
if (i === 3) {some code ...}
if (i === 7) {some other code ...}
 }

So obviously for 4 <= i <= 10 the condition i === 3 will always fail. Is there a way to achieve that if a condition is true for some i, this condition will not be checked any more for the other i's? Can this condition be deleted in some way? This would make the loop much faster.

(Of course the example of above does not make much sense and the real use case is much more complicated.)

Thank you in advance for your help!

Tall83
  • 343
  • 1
  • 4
  • 8
  • Store 3,7 in an array. Use for as in for that array. Then Switch case inside for. – Gibbs Mar 20 '17 at 04:57
  • That will probably slow it down. – meyer9 Mar 20 '17 at 04:58
  • How many conditions do you have there? it looks like you've direct equality conditions so if there are only two or three such conditions then wouldn't it be better to directly access the third element, seventh element and so on instead of looping the list? – Vishwa Bhat Mar 20 '17 at 04:59
  • If the items are mutually exclusive, there is really no way of speeding it up. Any complications will make the code much slower. – meyer9 Mar 20 '17 at 05:05
  • @meyer9 - `else if` would help a little though :p – Jaromanda X Mar 20 '17 at 05:07
  • As commented and the answer by Barak, the switch statement is the way to go with this. It's faster than if else if and does exactly what you are asking for. – creativekinetix Mar 20 '17 at 05:08
  • yep, that's true. switch is the way to go. – meyer9 Mar 20 '17 at 05:10
  • the performance difference between the **3** common methods (if else, switch, and *lookup table*) is minimal - *lookup table* is the fastest though, faster than both methods presented here - however, the difference is 2% at most – Jaromanda X Mar 20 '17 at 05:15
  • Thank you very much! Yes I will use one of the three methods. @creativekinetix: No, it's not exactly what I was asking for (I think I wasn't precise enough). These methods break the loop for a fixed i. But I was trying also to improve the performance for the larger i's as I know the specific condition cannot be true for one of them and don't need to be checked anymore. But as I can read from meyer9 this seems not to be possible in an effective way. – Tall83 Mar 20 '17 at 05:22
  • disregard the benchmark comments - clearly I found a flawed one !! It pays to take benchmarks written by other people with a grain of salt - if they don't know what they are doing, they can really skew the result ... one thing remains, switch is horribly slow in safari 9 at least - but is the way to go – Jaromanda X Mar 20 '17 at 05:25
  • @Tall83 what do you mean by "break the loop for a fixed i"? – creativekinetix Mar 20 '17 at 05:29
  • @JaromandaX - I agree lookup table is another good option. Especially if "some code..." is rather extensive. However, I wouldn't personally recommend it since the lookup value is an integer and not a string. – creativekinetix Mar 20 '17 at 05:35
  • @ creativekinetix: The loop starts with i = 1 and finishes with i = 10. If i = 3, the condition i === 3 is true. Then (using else if for example) the other condition i === 7 won't be checked for this specific i. Now the loop continues with i = 4. As the conditions are mutually exclusive there is no need to check the condition i === 3 any more (as this condition already had been true). So I was wondering whether I could delete a condition once it is true for some i. But this seems not to be the case. – Tall83 Mar 20 '17 at 05:40
  • @Tall83 - From a performance/optimization standpoint the switch is probably the best use in your case but even with the if/else the performance factor isn't going to be that apparent unless you are iterating over millions of lines of code. And in that case there might be a matter of seconds difference. Conditionals work pretty quick, especially straight equality comparisons. Good luck and happy coding. – creativekinetix Mar 21 '17 at 20:17

2 Answers2

1

Switch is better for what you're trying to achieve

for (i = 1; i <= 10; i++)
{
 switch(i){
  case 1:
   some code..;
   break; //once this is called, the statement will stop
  case 3:
   some other code..;
   break;
 }
}
Barak
  • 535
  • 6
  • 18
  • be aware, that in Safari 9, switch is 20 times **slower** than either if/else or *lookup table* – Jaromanda X Mar 20 '17 at 05:20
  • What about using break without switch? (more precisely I should use the continue statement) https://www.w3schools.com/js/js_break.asp – Tall83 Mar 20 '17 at 05:29
  • Switch statements with [multiple cases](http://stackoverflow.com/questions/13207927/switch-statement-multiple-cases-in-javascript#13207939) could also be of use in conjunction with the `continue` statement that is within the loop itself. – Barak Mar 20 '17 at 05:44
0

You can use else if statements to skip all of the other conditions once one is found.

for (i = 1; i <= 10; i++) {
    if (i === 3) {some code ...}
    else if (i === 7) {some other code ...}
}

In this case, if i is 3, the other conditions will be skipped.

meyer9
  • 1,120
  • 9
  • 26
  • So for i === 3 the other conditions would be skipped. However for i === 4 the first condition would unnecessarily be checked again (I know already that this condition cannot be true for the other i if it has already been true for some smaller i). This is my problem. – Tall83 Mar 20 '17 at 05:03
  • So, the items are mutually exclusive? – meyer9 Mar 20 '17 at 05:04
  • Yes, this is the case: For each i at most one of the conditions is true. And the other way round: Each condition is true for exactly one i. – Tall83 Mar 20 '17 at 05:09
  • There is no way to optimize it efficiently. Javascript is a pretty high level language so anything like trying to check if a value was compared before would significantly increase the time of your code. – meyer9 Mar 20 '17 at 05:09
  • You're right. My code was fine, but the wording was wrong thanks. – meyer9 Mar 20 '17 at 05:19