SoFunction
Updated on 2025-04-14

Tips for writing efficient AS3 code

Here are some of my test results

Array & Object constructing

When constructing arrays and objects, new Array() and new Object() are 3 times slower than [] and {}

Index Number type for Arrays

Number index type of array

ist[int(0)] faster than list[0]

Create Array vs. Updating Array

Avoid creating arrays multiple times in recirculating statements. It is best to create them once and replace them with multiple updates.
Nulling Array vs. Splicing Array

For huge arrays, splice operations are cost-effective, so try to avoid

When working with large Arrays splicing is obviously an expensive operation, you can avoid this by nulling the index and skipping it in a null scenario. If you need to splice in order to keep the Array length low. Then store these nulled indexes in another trash Array once the garbage count has reached a limit you've defined loop through the numerically sorted trash indexes deleting splices in bulk. This concept is demonstrated in Tweensy.

 

Nulling Object vs. Delete Object

Delete the property of an object is more expensive than setting the property to null, so it is best to set the property of an object to null.

Nesting Loops

Multiple nesting loops have poor efficiency, so it is best to ensure that the loop is within 2 layers.

 

Inline code vs. function references

If the function on the time frame is very long and the execution time is long, it is best to divide the function into multiple small functions to execute.

 

This can shorten execution time and improve efficiency

Arguments vs. variable referencing

Minimize the number of parameters of the function as much as possible

 

 

Function apply scoping do it or not?

Scoping is a little bit slower than not so if you don't have to then don't.

 

 

Array push vs. Array index

Use the method of setting index instead of using array function push

for example

list[] = data; 600% faster than using push directly;

 

Array emptying - length 0 vs. A new Array

If you need to set an empty array, there is a convenient way to choose, which is to set its length property to 0

Or you would think that doing this is a good choice because it saves memory, but in fact, the execution speed is not as efficient as the direct new array

Of course, if you need to clear more than 510 arrays as empty in a loop, it will be better if you set length to 0

 

Var declarations on multiple lines vs. Var declarations on a single line

 

Declaring variables in one line is better and more efficient than declaring multiple lines

.
var a:int=0, b:int=0, c:int=0;
vs.
var a:int=0;
var b:int=0;
var c:int=0;

 

Using Xor to swap variables

If you want to swap variables but don't want to create new variables, you can use xor

like:

 a = a^b;
b = a^b;
a = a^b;

 

Multiplication vs. Division

The calculation rate of multiplication is always faster than the start, for example, 5000/1000 is better than5000*0.001 is 130% faster;

 

 

Type casting comparison

When type casting the keyword as is 250% more efficient than casting by Type(item); Though surprisingly not using either is about 1400% more efficient.

 

It is recommended to use the corresponding type of variable for comparison

The same type is more efficient

 

 

Long vs Short variable names

Try to use short variable names