📜 ⬆️ ⬇️

Code Optimization [Actionscript].

Every time, starting a new project, many of us ask ourselves the question: what is the best way to write this or that part of the code? In this article I tried to collect my observations, tips on the forums on how to optimize the code on AS.

I hope these tips will be useful for you.

Cycle formation:
1) AS 3.0 is the fastest cycle
var i: int = 0
while (i <9999999) {
i + = 1 // exactly i + = 1
}

due to the typing in AS 3.0, there is a very large acceleration. In AS 2.0 this was not observed.
')
2) If there is no difference in which sequence to run through the array, then the fastest way to form a cycle will be:
var i = my_array.length;
for (i -) {
// actions on the element of the array my_array [i]
}

3) for ... in is much slower than for.


The following function is faster than the built-in Array.reverse () by 40-50 percent:
function reverse (a: Array) {
var l = 0;
var r = a.length-1;
var t;
while (l <r) {
t = a [l];
a [l] = a [r];
a [r] = t;
++ l;
--r;
}
}



with slows down, because it runs on with-hash, then on localvar-hash, then on this-hash. One run more. But in this and with are the keys of not only variables, but also functions.
with object {
...
}

(especially if there are a lot of calls to the SV-you and object'a methods) the algorithm slows down a lot.
It is better to abandon its use.


In large cycles, try to avoid using methods and ... arrays.

obj.x + = 1 // where obj is an instance of the class, in which the x member is declared // with the exact type var x: Number;

In AS 3.0, it will work an order of magnitude faster than

obj ['x'] + = 1 // where obj is an array

There is no difference in AS 1.0 / 2.0.


Reversal to a hash of local variables is faster than global ones, and even more so if search is used in sub-hashs. Therefore, when passing data through a function argument, everything happens faster. Hash something turns out from one variable consists. In general, local variables are addressed faster.


It is better to create intermediate variables for storing addresses in a hash. That is the type appeal
xxx.tr.mx.v + = xxx.tr.mx.dv

better lead to the following:
var mx: Object = xxx.tr.mx;
mx.v + = mx.dv;

thereby we get rid of two hash searches. Of course, everything is optimized in flash, but when it comes to shoveling huge arrays, each run over hash indexes causes brakes.


Array.length is decelerating, so it is better to first find out its length into a variable before starting the loop.


Math.random works 2-3 times slower than random.


a = b = c = 0 faster than a = 0; b = 0; c = 0;


Key.isDown (Key.SPACE) is slower than Key.isDown (32).


Avoid using this.


The syntax of the 4th Flash is faster than the call through “.”.


Save constant functions in local variables var Mpi = Math.PI.


Use var to define variables.


Use i ++ and i-- instead of i + = 1 and i- = 1.


Use where possible - instead of +.


Determining the coordinates of MC is much faster determined in this way:
backgroundMC._x = int (computedXposition);
backgroundMC._y = int (computedYposition);

What is this:
backgroundMC._x = computedXposition;
backgroundMC._y = computedYposition;


If there are any additions you can share with the rest.

Source: https://habr.com/ru/post/2964/


All Articles