Loops

From ActionScriptWiki

Jump to: navigation, search

There are several ways to loop through a list of elements. Sometimes the order or the variable of the iteration is not important so one can choose a different technique.

Contents

[edit] Loop Without Variable Access

This is the fastest version of a loop if there is no need to access the variable while iterating over a set of values.

const list: Array;
var i: int = list.length + 1;

while( --i != 0 )
	list.pop();

[edit] Fast Loop With Reverse Order

This is the fastest version of a loop if the order of the iteration may be reverse and access of the variable is necessary.

const list: Array;
var i: int = list.length;

while( --i > -1 )
	list[ i ].update();

[edit] Normal Loop

When using a normal loop there is a simple optimization by simply replacing i++ with ++i.

[edit] Wrong Version

for( i = 0; i < n; i++ )

[edit] Correct Version

for( i = 0; i < n; ++i )
Personal tools