Ok Here it is. I just spent the past 3 hours going through tons of sites, blogs and PDFs looking for good coding rules for AS3.
I have found 22 rules that are proven to improve performance but there could be more. Feel free to keep adding numbers eg.23,24 if you find any more rules that result in performance increases.
Hopefully the 3 hours I spent doing this will be repayed by not having to spend as much time refactoring inefficient code.
Actionscript Code Optimization Rules
1) Don't evaluate object/array lengths on each iteration of a loop
2) i=i+1 and i+=1 are faster than i++
3) Instead of dividing by 2, multiply by 0.5
Eg.
var newPosition:Number = parentObject.width * .5;
var len:int = manyObjects.length;
for (var i:int = 0 < len; i+=1)
{
manyObjects[i].x = newPosition;
}
4) never use Math.floor. It's slow as frak. Instead cast the number as a type int
5) don’t use Math.ceil. Cast to an int and add 1 int(1.5)+1
6) don’t use Math.abs() (distance of number from 0) instead use the following “if (n < 0) n = -n;” Where n is the number you want to find the absolute value of.
7) if you can do an operation outside a loop then do it!
Eg. Assign constant to variable to use in loop instead of doing assignment inside the loop.
8) declare variables on the same line
Eg var v1:Number=10, v2:Number=10, v3:Number=10, v4:Number=10, v5:Number=10;
9) use bitwise operations for division and multiplication
Eg.
var val1:int = 4 * 2;
var val2:int = 4 * 4;
var val3:int = 4 / 2;
var val4:int = 4 / 4;
becomes>
var val1:int = 4 << 1;
var val2:int = 4 << 2;
var val3:int = 4 >> 1;
var val4:int = 4 >> 2;
10) store objects retrieved from arrays in typed variables before calling properties and methods on it.
images[index].getPixel(x,y);
becomes>
var image: BitmapData = images[index];
image.getPixel(x,y);
11) General rule of thumb usage for dealing with numbers. (see end of post for more info)
Use ints whenever possible over Number.
Always use boolean over int whenever possible
Try not to use uint.
12) Coerce array index values to ints when index value is calculated.
Eg.
a[int(i*2)] = 1;
13) Do not use inner functions unless there is no alternative
14) Don’t put performance-intensive code in class initialization: (initialization functions are interpreted instead of JIT which is used for al other functions)
Eg.
class Sieve
{
var n:int, sieve:Array=[], c:int, i:int, inc:int;
set_bit(0, 0, sieve);
set_bit(1, 0, sieve);
set_bit(2, 1, sieve);
for (i = 3; i <= n; i++) set_bit(i, i & 1, sieve);
c = 3;
do { i = c * c, inc = c + c; while (i <= n) { set_bit(i, 0, sieve); i += inc; } c += 2;
while (!get_bit(c, sieve)) c++; } while (c * c <= n); }
15) Always used weak references for event listeners to aid garbage collection (final augment true)
Eg. stage.addEventListener(Event.CLICK,handleClick,fal se,0,true);
16) Create constants for commonly used objects, such as new Point(0,0), new Rectangle(0,0,320,240), etc. This reduces overhead of creating new objects.
private static const POINT:Point = new Point(0,0);
17) Reduce pointers to static class names as much as possible, use package variables and functions instead.
package somepackage{
public const somevar:int;
}
18) Store getter properties as local variables when using them more than once in a method (such as .graphics, .transform)
var n:Graphics = sprite.graphics;
n.clear();
n.beginFill(0x000000);
n.drawRect(0,0,10,10);
n.endFill();
another example>
//not so good
var person:Person;
if (person.firstName == “Matt” || person.firstName == “Ted” ||
person.firstName == “Alex”)
//much better
var fname:String = person.firstName;
if (fname == “Matt” || fname == “Ted” || fname == “Alex”)
19) Create custom reflection methods instead of using: getDefinitionByName(getQualifiedClassName(object))
public class SomeClass {
public function get reflect():Class {
return SomeClass;
}
}
var someclass:Class = object.reflect();
(this method is over 5000% FASTER!)
20) MAKE SURE EVERYTHING IS TYPED!
All variables, member and local
All function return values
All parameter types in functions or error handlers
All loop counters
If you see “var”, better see “:<type>”
Even if your class is dynamic
VM can take advantage of slots for members that are pre-compiled
New properties will require the hash lookup
21) If type is unknown, check before using.
Try coercing the variable to the type you want before accessing
An VM error can be 10x slower than the time it takes to execute the test or coercion.
22) Avoid spaces in arrays. (Keep them dense)
Eg. var a:Array = [1,2,3,4,5];
a[1000] = 2010; //bad, makes lookup slower
Performane of different number types for different assignments.
http://www.gskinner.com/blog/archive...in_as3_in.html
Assignment ( var a:TYPE = 0.5; )
int: 56-83ms
Number: 26-43ms
uint: 57-92ms
Predictably, Number is faster for fractional assignments, as the value does not need to be converted to an integer.
Division ( var a:TYPE = i/2; )
int: 60-105ms
Number: 34-64ms
uint: 184-278ms
Number is a much faster type for division, as expected from Sho's post. uint trails badly.
Multiplication ( var a:TYPE = i*2; )
int: 78-129ms
Number: 39-64ms
uint: 207-280ms
Similar results to division. I thought int might perform better as the value would never have to be converted to a float (whereas in division it would).
Addition ( var a:TYPE = i+2; )
int: 31-49ms
Number: 44-55ms
uint: 85-113ms
As expected from the plain iterator test, int is slightly faster for integer addition.
Bitshift ( var a:TYPE = i<<1; )
int: 31-63ms
Number: 61-114ms
uint: 71-130ms
int outperforms Number fairly handily in bit operations, this is likely because Number needs to be converted into an int to have bit operators applied. This is the only test so far that uint does passably well in (other than assignment).
Heres the source for all these rules
http://blog.andre-michelle.com/2005/...ns-suggestions
http://www.onflex.org/ACDS/AS3TuningInsideAVM2JIT.pdf
http://www.adobe.com/devnet/flashpla...anagement.html
http://www.adobe.com/devnet/flashpla...ollection.html
http://www.gskinner.com/blog/archive...in_as3_in.html
http://www.gotoandplay.it/_forums/viewtopic.php?t=179
http://lab.polygonal.de/2007/05/10/b...-integer-math/
http://www.onflex.org/ACDS/AS3TuningInsideAVM2JIT.pdf
http://www.danielhai.com/blog/?p=55
http://www.adobe.com/devnet/flex/art...fm_as3perf.pdf
http://rozengain.com/?postid=35
This desire for efficiency makes me hungry! Time for Dinner! I will add more rules if i find any more worth noting.