Browse All Articles > Tips,Tricks and Best Practices for Improving ActionScript Performance
Here are some practices and techniques that can be adopted into your Flash/Flex application development process.
Note: Not all "performance tips" provide an immediately-recognizable benefit. This article does not include timing validation data, does not cite sources, and does not indicate relative performance gain for each item. Nevertheless, it may be useful as a general guide. For time-critical operations, I recommend that you set up your own performance tests to verify the accuracy and applicability of these suggestions.
1
Avoid the new operator when creating Arrays.
var a = []; /// is better thanvar a = new Array();
Use static for properties methods that do not require an object instance.
StringUtils.trim( “text with space at end ” );Class definition:package {public final class StringUtils{ public static function trim( s : String ) : String { var trimmed : String; // implementation return trimmed;}}}
There is no difference in memory usage between an if statement and a switch statement:
if ( condition ){ // handle condition//--------------------------------------- IDENTICAL MEMORY USAGE:switch ( condition ){ case “A”: // logic to handle case A break; case “B”: // logic to handle case B break;}
Rank your if statements in order of comparisons most likely to be true
if ( conditionThatHappensAlot ) { // logic to handle frequently met condition} else if ( conditionThatHappensSomtimes ) { // handle the case that happens occasionally} else { // handle the case that doesn’t happen that often}
Frame Rate
Despite all discussions otherwise, there is no magic framerate. I use 25 or 30 because I like it best. At some point, I tested and determined one was slightly better than the other, but generally speaking this is not going to be the primary cause of a site running slow. I wouldn’t generally advise going higher than 30 though, just because you’re asking the player to render an awful lot awfully quickly…
15
Use ENTER_FRAME events instead of Timer events wherever possible.
16
visible is better than alpha.
alpha = 0 and visible = false are totally different things. Alpha determines the opacity of a clip. Visible determines whether or not the clip should actually be rendered by the player. If you’re setting something all the way invisible, use the visible property.
17
Use try...catch wherever possible.
Each try, catch, and finally (optional) represent three different types of code blocks that can be used within a try..catch..finally statement.
18
Multiply vs. Divide:
For instance, use – 5000*0.001
instead of – 5000/1000
19
Use RegEx for validation. Use string methods for searching.
20
Reuse objects to maintain a “memory plateau” DisplayObjects, URLLoader objects.
21
Follow the Flex component model for custom components for better performance:
Avoid the setStyle() method -- It is one of the most expensive calls in the Flex framework.
23
Try to avoid Repeaters for scrollable data.
24
cacheAsBitmap and BitmapData
Where possible, use cacheAsBitmap to rasterize vector items. You’ll force Flash to draw the symbol one time and then never again. On the flip side, if you’re scaling or rotating a symbol, NEVER set it to cacheAsBitmap. Then you force Flash to render AND recapture the raster every frame, making the process slower instead of faster.
25
Use Vectors for fixed data typed Arrays
I highly recommend reading this very thorough Flash Player 10.1 Performance review in several parts by Jackson Dunstan, an AS3 programmer. It describes similarities and differences in all major aspects of performance between Flash Player 10.0 and 10.1, documented by data from additional articles.
Comments (0)