General ActionScript 2 Guidelines
The following optimizations will increase AS2 execution speed.
Loops
- Focus on optimizing loops, and any repeating actions.
- Limit the number of loops used and the amount of code that each loop contains.
- Stop frame-based looping as soon as it is no longer needed.
- Avoid calling a function multiple times from within a loop.
- It is better to include the contents of a small function inside the loop.
Functions
- Whenever possible, avoid deeply nested functions.
- Do not use with statements inside functions. This operator turns off optimizations.
Variables / Properties
- Avoid referencing nonexistent variables, objects, or functions.
- Use the “var” keyword whenever possible. The use of the “var” keyword inside functions is especially important since the ActionScript compiler optimizes access to local variables using internal registers with direct access by index rather than putting them into a hash-table and accessing by names.
- Don't use class variables or global variables when local variables will suffice.
- Limit the use of global variables, because they are not garbage collected if the movie clip that defined them was removed.
- Delete variables or set them to null when no longer needed. Doing this marks the data for garbage collection. Deleting variables helps optimize memory use during runtime, because unneeded assets are removed from the SWF file. It is better to delete variables than to set them to null.
- Always try to access properties directly rather than using AS getter and setter methods, which have more overhead than other method calls.