Tips for writing efficient AS3 code _flash AS3

Source: Internet
Author: User
Tags array length type casting
Here are some of my test results

Array & Object Constructing

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

Index Number type for Arrays

Numeric index types for arrays

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

Create array vs. updating array

To avoid creating arrays more than once in a looping statement, it is best to create one time replacement with multiple update content
Nulling array vs. splicing array

For a large array of splice operations are cost-intensive, so try to avoid

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

Nulling object vs. Delete Object

It is more expensive to delete an object's properties than to set the property to NULL, so it is best to set the property of an object to null

Nesting Loops (Nested loops)

Multiple nesting cycle efficiency is poor, so it is best to ensure that the cycle within 2 layers

Inline code vs. function references

If the function on the time frame is long and executes for a long time, it is best to divide the function into smaller functions.

This will shorten execution time and improve efficiency

Arguments vs. variable referencing

Minimize the number of parameters of a function

Function apply scoping does it or not?

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

Array push vs. array index

Instead of using an array function to push by setting the index

Like what

List[list.length] = data; 600% faster than direct use of push;

Array emptying-length 0 vs. A new Array

If you need to set an empty array, there is a convenient way to select it by setting its length property to 0

Or you might think it's a good choice because it saves memory, but in fact it does not perform as efficiently as the direct new array.

Of course, if you need to clear more than 510 arrays in a single loop to be empty, it's better to set the length to 0.

var declarations on multiple lines vs. var declarations in a single

Declaring a variable in a row is better and more efficient than declaring multiple rows

i.e.
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

如果你想去交换变量,但是又不想创建新的变量的时候,可以用xor

如:

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

Multiplication vs. Division

The multiplication rate is always faster than the start, like 5000/10005000*0.001快130%;

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.

 

建议使用对应的类型的变量进行比较

同类型的比较效率高的多

 

 

Long vs Short variable names

Try to use a short variable name

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.