Local Variables

From ActionScriptWiki

Jump to: navigation, search

There are some myths flying around when it comes to local variables and where they should be placed. The answer is that whenever it makes sense to put a local variable in a loop it should be done. The reason is that the ActionScript Compiler initializes all local variables at the beginning of a method and it does not matter where they have been placed in the source code.

[edit] Example

Both examples will have the same running time because the compiler will initialize n2 at the beginning of the method only once.

var n: int = 0x10;
var n2: int;
while(--n != 0)
{
	n2 = n << 1;
	trace( n2 );
}
var n: int = 0x10;
while(--n != 0)
{
	var n2: int = n << 1;
	trace( n2 );
}
Personal tools