The past few weeks have been really nice. I’ve been working on a number of freelance jobs which have involved little to no programming and have also been managed quite well so that I’ve had a little more spare time in my evenings. This has been nice because I’ve spent a few hours researching and reading about technique to improve and optimise my ActionScript code.
Once of the best ways I have found to really learn something is to copy it from it’s original source and write it down for my own notes. This is exactly what I plan to do with this post. I will be wrtiting down a series of tips and tricks as well as posting the original articles that I got them from. Some of these articles may go in to even more detail about these techniques and suggest others, so I would definitely not treat this post as a definitive list, it is simply an overview of some of the key points that are tried and tested techniques.
General.
- Try not to overload conditional statements ie.
if(something && somethingElse && somethingElseToo) - Do not use Objects if you know which properties will be finally involved
- Cast instances while reading from an Array
- Try to use integers (int) for all possible cases
- Use bit operators where possible
- Use
++for incrementing and- nfor decrementing - Use multiplication instead of division
- Use
int()instead ofMath.floor()for positive numbers - Use
n < 0 ? n * -1 : ninstead of Math.abs() - Declare multiple variables on a single line where possible
- Use
{}and[]instead ofnew Object()ornew Array() myArray[int(i)]is faster thanmyArray[i]- Use
myArray[int(i)] = nullinstead ofmyArray.splice(i, 1)where possible and skip in a null scenario - Use
nullinstead ofdeleteif you can avoid it - try to avoiding nesting functions within functions and try to reduce the amount of seperate functions you call in an
ENTER_FRAMEevent orforloop for example - Use a variable with a stored value instead of arguments in a function where possible
Arrays.
- Use int as the incremental value
- Store the length in a variable outside the for loop
- Store common values outside of a for loop
- Use
myArray[i] = myObjinstead ofmyArray.push(myObj)
var l : int = myArray.length;
for(var i : int = 0; i < l; i++) {}
Links.
- http://osflash.org/as3_speed_optimizations
- http://www.rozengain.com/blog/2007/05/01/some-actionscript-30-optimizations/
- http://www.adobe.com/devnet/flashplayer/articles/resource_management.html
- http://www.adobe.com/devnet/flashplayer/articles/garbage_collection.html
- http://www.gskinner.com/blog/archives/2006/06/types_in_as3_in.html
- http://lab.polygonal.de/2007/05/10/bitwise-gems-fast-integer-math/
- http://lab.polygonal.de/2007/07/18/fast-and-accurate-sinecosine-approximation/
- http://blog.haxe.org/entry/26
- http://agit8.turbulent.ca/bwp/2008/08/04/flash-as3-optimization-fastest-way-to-copy-an-array/
- http://www.gskinner.com/blog/archives/2008/12/making_dispatch.html
- http://www.lostinactionscript.com/blog/index.php/2008/09/28/tips-on-how-to-write-efficient-as3/