Forum Moderators: open

Message Too Old, No Replies

if, if vs. if, else if

         

peterinwa

6:18 pm on Sep 11, 2004 (gmt 0)

10+ Year Member



If I have a long series of "if" statements and I know that only one will be true, is it advantageous to make all but the first "else if" statements?

After finding a true statement, does the browser then skip over the remaining "else if" statements without analyzing them and therefore run faster?

If not, the additional text would just makes the code load slower.

Thanks, Peter

j4mes

7:18 pm on Sep 11, 2004 (gmt 0)

10+ Year Member



I really don't know for sure how a browser deals with else statements after it's found one that's true (without wandering through a lot of source code :P) but almost certainly it stops and does something else instead.

and therefore run faster?

Either way, it really isn't important with any computer sold within the last decade, since your code probably isn't going to need a supercomputer to process it, the difference will be nothing compared with download speed, etc.

Really, so long as you know that two if statements wont both be true at the same time (and cause an error) then go with whatever you want, whether that's shorter code or something easier to read or whatever.

James

Gorilla

7:26 pm on Sep 11, 2004 (gmt 0)

10+ Year Member



The main benefits from using "else if" type constructs are:

1) faster execution. the code runs faster because after a test has been found to be true, none of the following test cases will be evaluated. since javascript is interpreted, the code may still have to be parsed.

2) more robust code. it's less likely that you will have a bug where the code protected by two different if-statements will be executed.

If your program is including a series of "else if" statements, you might also want to consider using the javascript switch statement.

Rambo Tribble

4:27 pm on Sep 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A word about code efficiency: Modern hardware, with speeds and address spaces that were all but unimaginable even just a decade ago, has led many to assuming that minor differences in code efficiency are of no consequence. This is based on a rather egotistical assumption: That the user has nothing better to do than run your code in exclusivity. With modern, multi-tasking operating systems, this is a fallacious assumption. What if the user is simultaneously running multiple, processor-intensive applications? What if each of those applications was built with this disregard for code optimization?

In the hoary old days of yore, each processor cycle was precious. While hardware improvements may have diminished the importance of a single cycle, it is still a resource the worth of which can only be gained when it isn't wasted.

Hanu

5:28 pm on Sep 12, 2004 (gmt 0)

10+ Year Member



Also consider this: What if your conditions are expensive (take a lot of time to execute) or have side-effects like incrementing a variable? Then it matters whether you use "if if" or "if else if". On the other hand, if you already know that only one condition can be true, you should use "if else if".

BUT: The most time consuming activity when executing JavaScript is actually parsing and interpreting it. Whether you use "if if" or "if else if" does not affect parsing because the whole construct has to be parsed. In fact, "if else if" takes a teeny weeny bit longer to parse because it's more text.

In reality you won't usually notice a difference but for reasons already mentioned in this thread it's considered good style to use "else if" when the conditions are disjunctive (only one can be true at a time) or partially disjuntive (only one of a subset of the conditions can be true at a time).

Oh, and by using "else if" you also indicate the disjunctiveness of your conditions to a person reading your code, helping them understand it. Very important.

j4mes

7:28 pm on Sep 12, 2004 (gmt 0)

10+ Year Member



What if the user is simultaneously running multiple, processor-intensive applications?

Or Windows? ;)