Schemaphic Systems Blog

Sharing experiences in Flash, Flex and AIR

Tips & Tricks for improving AS3.0 performance

with 2 comments

Here are some filtered practices and techniques that can be adopted into flash/flex application development process:

1. Avoid the new operator when creating Arrays
use –
var a = [];
instead of – var a = new Array();
Same for creating Objects.

2. Fastest way to copy an array:
var copy : Array = sourceArray.concat();

3. 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;

}}}

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

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

6. Length of method/variable names doesn’t matter in AS3 (true in other langs)
someVeryLongMethodNameDoesntReallyImpactPerformanceMuch();

7. One line assignments DO NOT buy any performance (true in other langs)
var i=0; j=10; k=200;

8. 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;

}

9. 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 occaisonally

} else {

// handle the case that doesn’t happen that often

}

10. AVM promotes int to Number during calculations inside loops

11. Use integers for iterations
use –
(var i: int = 0; i < n; i++)
instead of – (var i: Number = 0; i < n; i++)

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

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

14. Frame Rate
Despite all discussions otherwise, there is no magic framerate. I use 25 or 30 because (as far as I know) 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 fast…

15. Use ENTER_FRAME events instead of Timer events where ever 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 where ever possible
Each try, catch, and finally (optional) represent 3 different types of code blocks that can be used within a try..catch..finally statement.

18. Multiply vs. Divide:
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:
createChildren();
commitProperties();
updateDisplayList();

22. Avoid the setStyle() method (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


Advertisements

Written by Deepanjan Das

May 27, 2010 at 2:48 PM

2 Responses

Subscribe to comments with RSS.

  1. Great!
    And what about cloning objects , for example what if i have thousands points to create , what i have to do , create one and in loop use its setTo method with specific coordinates and then clone this point object or create new Point in each loop iteration?

    Ivan

    April 12, 2013 at 1:07 PM

  2. Good one… Very helpful!

    Kiru

    June 5, 2014 at 12:13 AM


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: