Copyright © 2004 O'Reilly Media, Inc. All Rights Reserved.
|This content is excerpted from the above-named O'Reilly publication, with permission, by agreement with ActionScript.org.|
Flash's ubiquity is largely attributable to the small download size of the Flash Player and the ability to create low-bandwidth content. However, the filesize optimization comes at the cost of reduced performance as you increase complexity.
Vectors graphics are more compact than other image formats because vectors define the math (points, curves, and fills) required to draw the image at runtime rather than the raw graphic data. However, converting the vector data to the final image is time consuming and must be done whenever there is a change in appearance or position of a graphic. If your image contains complex shape outlines or large fill areas, and they also change every frame, your animations might run slowly.
In this chapter, we explore several ways of reducing the performance hit caused by using vectors, including how to design graphics [Hack #68] and when to substitute bitmaps for vector images [Hack #72] .
ActionScript is not compiled to
native machine code like some languages (such as C++). Instead it is
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:
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.