We know that AS3 uses lazy-evaluation in conditional expressions. That’s to say, a conditional expression stop checking as soon as possible. We can see that in action pretty easily

[as3]function a():Boolean {
trace("a()");
return false;
}
if(a() && a());[/as3]

There will only be one trace of “a()” – meaning the second function never got called because it didn’t need to be. So a conditional expression using operators should be equivalent of statement blocks. That’s to say that this:

[as3]if(a && b && c && d)[/as3]

Should be functionally equivalent to:

[as3]if(a) if(b) if(c) if(d)[/as3]

Behaviorally, this is true. But…

While AS3 suppresses the behavior, the conditional expression appears to be ‘examined’ – if not evaluated – for each operand. I presume each operand is pushed into the stack, whether or not it ever gets evaluated, which consumes resources.

Note the results of the following basic benchmarks:

[as3]var start:Number = getTimer();
for(var i:int=0;i<500000;i++){
if(false && true && true && true){}
}
trace(getTimer()-start);[/as3]

The above code runs about 10ms on my laptop. Compare that to the following, which should be functionally identical:

[as3]var start:Number = getTimer();
for(var i:int=0;i<500000;i++){
if(false) if(true) if(true) if(true){ };
}
trace(getTimer()-start);[/as3]

Which runs about 4ms – less than half the time.

Running the same benchmarks in JavaScript show that JS does use truly lazy evaluation. Compare the following:

[as3]var start = new Date();
for(var i=0;i<500000;i++){
if(false) if(true) if(true) if(true){}
}
alert(new Date()-start);[/as3]

Runs an average of 330 ms, while:

[as3]var start = new Date();
for(var i=0;i<500000;i++){
if(false && true && true && true){}
}
alert(new Date()-start);[/as3]

Runs an average of 295 ms. Note that execution times are close enough to show that both approaches are effectively equivalent, but interestingly the use of conditional operators (versus conditional statement blocks) is slightly faster.