Tips for writing efficient as3 code

Source: Internet
Author: User
Tags type casting

Below are some of my test results
Array & object constructing

When constructing arrays and objects, new array () and new object () are three times slower than [] and {}.

Index Number Type for Arrays

Array numeric index type

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

Create array vs. Updating Array

Avoid creating arrays multiple times in a recycling statement. It is best to create an array at a time and replace it with the updated content multiple times.
Nulling array vs. Splicing Array

For a large array, the row-based splice operation is costly and should be avoided as much as possible.

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

Deleting an object attribute is more expensive than setting this attribute to null. Therefore, it is best to set this attribute to null.

Nesting loops (nested loop)

The efficiency of multiple nested loops is poor, so it is best to ensure that the loops are within two layers.

Inline code vs. Function references

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

This can shorten the execution time and improve the efficiency.

Arguments vs. Variable referencing

Minimize the number of function parameters

Function apply scoping do it or not?

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

Array Push Vs. Array Index

Use the index setting method instead of the Array Function push.

For example

List [list. Length] = data; 600% faster than direct push;

Array emptying-length 0 vs. A new array

If you need to set an empty array, you can easily select it by setting its Length attribute to 0.

Or you may think this is a good choice because it can save 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 empty arrays in a loop, it will be better to set the length to 0.

VaR declarations on multiple lines vs. var declarations on a single line

Declaring variables in one 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

If you want to exchange variables without creating new variables, you can use XOR

For example:

A = a ^ B;
B = a ^ B;
A = a ^ B;

Multiplication vs. Division

The speed of multiplication is always faster than that of departure, for example, 5000/1000.5000*0.001 fast 130%;

Type Casting comparison forced conversion type comparison

When type casting the keywordAsIs 250% more efficient than castingType (item );Though surprisingly not using either is about 1400% more efficient.

We recommend that you use corresponding types of variables for comparison.

The same type is more efficient.

Long vs short variable names

Use a short variable name whenever possible

Related Article

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.