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.


Avoid the new operator when creating Arrays.
var a = [];            /// is better than
                      var a = new Array();

Open in new window

The same applies for creating Objects.


The fastest way to copy an array is:
var copy : Array = sourceArray.concat();

Open in new window


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;

Open in new window


Use const for properties that will never change throughout the lifecycle of the application.
public const APPLICATION_PUBLISHER : String = “Company, Inc.”;

Open in new window


Use final when no subclasses need to be created from a class.
public final class StringUtils

Open in new window


The length of method/variable names doesn’t matter in AS3 (true in some other languages).

Open in new window


One line assignments DO NOT buy any performance (true in some other languages).
var i=0; j=10; k=200;   // ... is no faser than if on separate lines

Open in new window


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
                          case “B”:
                          // logic to handle case B

Open in new window


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

Open in new window


AVM promotes int to Number during calculations inside loops.


Use integers for iterations:
(var i: int = 0; i < n; i++)        // is better than...
                      (var i: Number = 0; i < n; i++)

Open in new window


When dealing with indices, use the value -1 to mean “no index”.


Locally store function values in for and while statements instead of repeatedly accessing them.
var toRadians:Number = a*180/Math.PI;
                      for (..){ b* toRadians; }

Open in new window


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…


Use ENTER_FRAME events instead of Timer events wherever possible.


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.


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.


Multiply vs. Divide:
For instance, use – 5000*0.001
instead of – 5000/1000


Use RegEx for validation.  Use string methods for searching.


Reuse objects to maintain a “memory plateau” DisplayObjects, URLLoader objects.


Follow the Flex component model for custom components for better performance:

Open in new window


Avoid the setStyle() method -- It is one of the most expensive calls in the Flex framework.


Try to avoid Repeaters for scrollable data.


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.


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.

    Flash Player 10.1 Performance: Part 1
    Flash Player 10.1 Performance: Part 2
    Flash Player 10.1 Performance: Part 3

Note: This article is reprinted from my blog.

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.