Increase Code Performance

ActionScript is not compiled to native machine code like some languages (such as C++). Instead it is converted to bytecode, which is faster than an interpreted language but not as fast as native code. Although ActionScript can be slow, in most multimedia presentations, the assets such as graphics, audio, and video?and not the code?are often the limiting performance factor. Furthermore, the overall performance (or apparent performance from a user perspective) is also affected by download times and not just runtime execution. Of course, for a Rich Internet Application accessing a back-end database, performance might depend on the speed of database queries, data transmission, and text rendering.

Many hacks throughout this book cover ActionScript optimization. Furthermore:

  • Flash Player 7 is already optimized for code that uses lots of function calls and local variables [Hack #100], and older performance tricks are no longer as necessary.

  • Many optimization techniques are not specific to ActionScript but simply well-known techniques for writing code in any language without an optimizing compiler. For example, loops are faster if you remove items that don't change with every loop iteration and place them outside the loop instead.

  • If your code runs slowly, it is often a sign that you need to reduce the scope of your application or look for a hack that solves the problem in a different way. You should identify and remove bottlenecks, for example, by optimizing your graphics.

  • Often performance is about perception [Hack #71] . If you attempt to perform too much work in a single frame, Flash doesn't have time to render the Stage, and the user perceives a slowdown. If you break up the amount of work being performed into smaller chunks, Flash can refresh the Stage at the prescribed frame rate, and there is no perceived slowdown.

Although properly written ActionScript doesn't usually constitute a bottleneck, improvements can always be made. For those with an interest in ActionScript optimization, have a look at the following links:

  • gotoAndPlay ( offers a tutorial on ActionScript optimization.

  • Odd Hammer ( offers performance benchmarks of various ActionScript optimization techniques in different versions of the Flash Player.

Throughout this chapter, we'll explore ways to analyze and optimize performance in areas that are most likely to meaningfully affect runtime performance, download times, and the user experience. As with most topics, the optimal techniques depend on the situation, and there is often a trade-off between, say, performance versus convenience or download times versus runtime performance. The following hacks will help you make intelligent choices for your individual projects.

You can find numerous other optimization-related hacks throughout the book, such as how to optimize sound [Hack #58] and optimize character animations [Hack #28] .