ActionScript and Flash Player optimization 763
Optimizing your code
Remember the following guidelines when you optimize your code:
■ Avoid calling a function multiple times from within a loop.
It is better to include the contents of a small function inside the loop.
■ Use native functions when possible.
Native functions are faster than user-defined functions.
■ Don’t overuse the Object type.
Data-type annotations should be precise, because it improves performance. Use the
Object type only when there is no reasonable alternative.
■ Avoid using the eval() function or array access operator.
Often, setting the local reference once is preferable and more efficient.
■ Assign the Array.length to a variable before a loop.
Assign
Array.length to a variable before a loop to use as its condition, rather than using
myArr.length itself. For example,
var fontArr:Array = TextField.getFontList();
var arrayLen:Number = fontArr.length;
for (var i:Number = 0; i < arrayLen; i++) {
trace(fontArr[i]);
}
instead of:
var fontArr:Array = TextField.getFontList();
for (var i:Number = 0; i < fontArr.length; i++) {
trace(fontArr[i]);
}
■ Focus on optimizing loops, and any repeating actions.
Flash Player spends a lot of time processing loops (such as those that use the
setInterval() function).
■ Add the var keyword when declaring a variable.
■ Don’t use class variables or global variables when local variables will suffice.