ActionScript optimisation tips and tricks

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 - n for decrementing
  • Use multiplication instead of division
  • Use int() instead of Math.floor() for positive numbers
  • Use n < 0 ? n * -1 : n instead of Math.abs()
  • Declare multiple variables on a single line where possible
  • Use {} and [] instead of new Object() or new Array()
  • myArray[int(i)] is faster than myArray[i]
  • Use myArray[int(i)] = null instead of myArray.splice(i, 1) where possible and skip in a null scenario
  • Use null instead of delete if 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_FRAME event or for loop 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] = myObj instead of myArray.push(myObj)
var l : int = myArray.length;
for(var i : int = 0; i < l; i++) {}

Links.