# Optimizing if() statement

**URL:** <https://forum.kirupa.com/t/optimizing-if-statement/263531>\
**Category:** Uncategorized\
**Created:** [June 16, 2008, 10:03pm UTC](https://forum.kirupa.com/t/optimizing-if-statement/263531 "2008-06-16T22:03:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![phena](https://avatars.discourse-cdn.com/v4/letter/p/e9bcb4/32.png) [@phena](https://forum.kirupa.com/u/phena)\
**Post date:** [June 16, 2008, 10:03pm UTC](https://forum.kirupa.com/t/optimizing-if-statement/263531/1 "2008-06-16T22:03:34Z")

</div>

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 😏

---

<div class="post-metadata">

**Author:** ![Blommestein](https://avatars.discourse-cdn.com/v4/letter/b/9d8465/32.png) [@Blommestein](https://forum.kirupa.com/u/Blommestein)\
**Post date:** [June 16, 2008, 10:11pm UTC](https://forum.kirupa.com/t/optimizing-if-statement/263531/2 "2008-06-16T22:11:22Z")

</div>

In theory they should be identical. Take a look at this: [http://en.wikipedia.org/wiki/Short-circuit\_evaluation](http://en.wikipedia.org/wiki/Short-circuit_evaluation)

---

<div class="post-metadata">

**Author:** ![glosrfc](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/glosrfc/32/8334_2.png) [@glosrfc](https://forum.kirupa.com/u/glosrfc)\
**Post date:** [June 16, 2008, 10:41pm UTC](https://forum.kirupa.com/t/optimizing-if-statement/263531/3 "2008-06-16T22:41:54Z")

</div>

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:

```auto
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));
}

```
