Schemaphic Systems Blog

Sharing experiences in Flash, Flex and AIR

Archive for the ‘Code Optimization’ Category

Flash Player 10.1 Performance Article

with 2 comments

A 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
Flash Player 10.1 Performance: Part 4
Flash Player 10.1 Performance: Part 5

Advertisements

Written by Deepanjan Das

October 29, 2010 at 7:22 AM

Pseudo Threading in Actionscript

leave a comment »

I came accross pseudo threading concept in ActionScript.
Alex Harui provided the Pseudo Threading concept for Flex and Joris Timmerman is the one to make it available for flash as well.

Thanks guys till Adobe make it available within.

Written by Deepanjan Das

July 20, 2010 at 7:55 PM

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


Written by Deepanjan Das

May 27, 2010 at 2:48 PM