Optimizing if() statement

Ive allways wondred if or what the diffrence would be (with thought on speed) in the following code:

ActionScript Code:
[LEFT]</p>
<p>function SomeThingCOLOR=#000000[/COLOR] [COLOR=#000000]{[/COLOR]</p>
<p> [COLOR=#0000ff]if[/COLOR][COLOR=#000000]([/COLOR]var1 == var2 && var3==var4 && var5 == var6[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]</p>
<p> [COLOR=#0000ff]return[/COLOR] [COLOR=#000000]true[/COLOR];</p>
<p> [COLOR=#000000]}[/COLOR]</p>
<p>[COLOR=#000000]}[/COLOR]</p>
<p>
[/LEFT]

and

ActionScript Code:
[LEFT]</p>
<p>function SomeThingCOLOR=#000000[/COLOR] [COLOR=#000000]{[/COLOR]</p>
<p> [COLOR=#0000ff]if[/COLOR][COLOR=#000000]([/COLOR]var1 == var2[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]</p>
<p> [COLOR=#0000ff]if[/COLOR][COLOR=#000000]([/COLOR]var3 == var4[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]</p>
<p> [COLOR=#0000ff]if[/COLOR][COLOR=#000000]([/COLOR]var5 == var6[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]</p>
<p> [COLOR=#0000ff]return[/COLOR] [COLOR=#000000]true[/COLOR];</p>
<p> [COLOR=#000000]}[/COLOR]</p>
<p> [COLOR=#000000]}[/COLOR]</p>
<p> [COLOR=#000000]}[/COLOR]</p>
<p>[COLOR=#000000]}[/COLOR]</p>
<p>
[/LEFT]

personally ive allways used example #2 as that is the way i would think fastest, but im no computer :smirk:

In theory they should be identical. Take a look at this: http://en.wikipedia.org/wiki/Short-circuit_evaluation

In practice, they won’t be identical. It depends on whether every condition is true, or if any of them false. Assuming that you expect the conditions to evaluate as true, then inline conditional if statements are the way to go. But, if you expect it to be more likely that one or more of your conditions will evaluate as false, you should nest your if statements. Try experimenting with the following code:

testOne();
testTwo();
testThree();
testFour();
// ------------------------------------
function testOne() {
	var a = true;
	var b = true;
	var c = true;
	var d = true;
	startTime1 = getTimer();
	for (var i = 0; i < 100000; i++) {
		if (a && b && c && d) {
			// do something
		}
	}
	trace("All inline conditions are true = " + (getTimer() - startTime1));
}
// ------------------------------------
function testTwo() {
	startTime2 = getTimer();
	for (var i = 0; i < 100000; i++) {
		if (a) {
			if (b) {
				if (c) {
					if (d) {
						// do something
					}
				}
			}
		}
	}
	trace("All nested conditions are true = " + (getTimer() - startTime2));
}
// ------------------------------------
function testThree() {
	var a = false;
	var b = true;
	var c = true;
	var d = true;
	startTime3 = getTimer();
	for (var i = 0; i < 100000; i++) {
		if (a && b && c && d) {
			// do something
		}
	}
	trace("Condition a is false (inline) = " + (getTimer() - startTime3));
}
// ------------------------------------
function testFour() {
	var a = false;
	var b = true;
	var c = true;
	var d = true;
	startTime4 = getTimer();
	for (var i = 0; i < 100000; i++) {
		if (a) {
			if (b) {
				if (c) {
					if (d) {
						// do something
					}
				}
			}
		}
	}
	trace("Condition a is false (nested) = " + (getTimer() - startTime4));
}