- Use native code instead of AS3Generally, it’s better to let Flash Player’s native code do the heavy lifting rather than using AS3. The reason for this is that the VM that AS3 code runs in necessarily has some overhead to it compared to the functions you can call in the Flash Player API (everything in the
flash.*packages). One exception to this rule is when the API does something you want to avoid such as allocate memory.
- Eliminate allocations to reduce GCIn addition to the allocations that you expect—such as those that you trigger by using the
newoperator—there are tons of hidden allocations such as
Stringobjects from concatenation, objects the Flash Player creates such as
Events. These allocations are slow and collecting them when you’re done with them is even slower, so try to get rid of them.
- Reuse objects to reduce GCWhen you’re done with objects, Flash Player’s garbage collector will reclaim their memory for reuse later. Unfortunately, this process is very slow and it’s hard to control when it will happen. Instead of making new objects, consider reusing existing ones. One technique that can help with this is called free lists.
- Don’t use anything dynamicThat includes dynamic functions (e.g. anonymous functions and functions declared as local variables), objects such as
operator for accessing fields, and untyped (
*) variables. All of these are much slower than their static equivalents such as regular methods, non-dynamic classes, the dot operator, and typed variables.
- Offload to the GPUThese days in Flash you have the ability to use video cards’ GPUs in addition to the main CPU. Using a combination of these is the key to high performance with 3D graphics (
Stage3D) and high-definition video (
- Reduce function callsFunction calls are, very unfortunately, quite slow in AS3. This includes getters and setters, which occur all the time (e.g.
Array.length). Try caching the results of functions instead of calling them more than once, especially with getters. In extreme cases, manually inline the body of functions into one larger function.
- Use speciality functions and classes rather than general purpose ones
Spriteuses less memory than
Shapeuses less memory than
BitmapData.copyPixelsis faster than
- Use fewer static accessesAccessing static variables, constants, and functions is slower than accessing their non-static counterparts. Consider using non-static alternatives or caching those static accesses as either non-static variables/constants or local variables/constants.
- Prefer local variables to fieldsReading and writing class and object variables (a.k.a. fields, member variables) is much slower than accessing local variables. Cache field accesses as local variables when you use them a lot.
- Eliminate pointless codeIt’s common to see variables initialized to their default value, which slows down the creation of objects and the execution time of functions. Get rid of this code that does nothing as a matter of good habit and the performance advantages will add up across your entire app.