Post: as3.0 Performance Optimization

Source: Internet
Author: User
Tags string methods

Some time ago I saw an article about performance optimization. Today I have a good time to take a look at it. There are many basic and worth noting. By the way, I have translated some of the main conclusions, if you are not familiar with this aspect, you can take a look at it.
.
 
1. Avoid the new operator when creating Arrays

VaR A = [];
Not:

VaR A = new array ();
Do not use a new array. Use [];
Fp10 supports the type array vector.
In an article, we can see that vector is nearly 60% faster than array.
 

2. arrays are expensive to create, do so conservatively

VaR vanitycollection01: array = new array ();
VaR vanitycollection02: array = new array ();
VaR vanitycollection03: array = new array ();
VaR vanitycollection04: array = new array ();
Arrays are resource-consuming and can be used as appropriate.
 
3. Fastest way to copy an array:

VaR copy: array = sourcearray. Concat ();
The fastest way to copy an array is to use Concat ().
 
4. setting values in arrays is slow

Employees. Push (employee );
Employees [2] = employee;
It is very slow to assign values to array elements.
 
5. Getting values from arrays is twice as fast as setting

VaR EMPLOYEE: Employee = employees [2];
Getting from an array is twice the speed of setting.
 
6. use static for properties methods that do not require an object instance

?


View code
Actionscript3
StringUtils.trim( "text with space at end " );
Class definition:
package{ public final class StringUtils
{
public static function trim(s :String):String { var trimmed :String; // implementation... return trimmed; } }}

Try to use static attributes and functions instead of relying on an instance
In particular, Function
 
7. Use const for properties that will never change throughout the lifecycle of the application

Public const application_publisher: String = "Company, Inc .";
Use const to declare the property variables that will never be changed in those programs
 
8. Use final when no subclasses need to be created of A Class

Public final class stringutils
Use final to define classes without subclasses
 
9. Length of method/variable names doesn't matter in ActionScript 3.0 (true in other Langs)

Somecrazylongmethodnamedoesntreallyimpactperformancetoomuch ();
Long variables/function names are not affected by program execution. (Other languages are also used)
 
10. One line assignments do not buy any performance (true in other Langs)

VaR I = 0; j = 10; k = 200;
Declaring several variables in one row does not improve performance. (Other languages are also used)
 
11. No difference in memory usage between an if statement and a switch statement

?


View code
Actionscript3
if( condition ){     // handle condition}
IDENTICAL MEMORY USAGE:switch( condition )
{ case "A":
// logic to handle case A
break;
 
case "B": // logic to handle case B break;
}

There is no difference in memory usage between If and switch statements.
 
12. Rank your if statements in order of comparisons most likely to be true

?


View code
Actionscript3
if(conditionThatHappensAlot ){     // logic to handle frequently met condition}else if(conditionThatHappensSomtimes ){    // handle the case that happens occaisonally}
else{ // handle the case that doesn’t happen that often}

Put the condition with a high probability of true in front.
 
13. AVM promotes int to number during Calculations Inside loops (Vm has
Been changing, from 9 to 10, so int, uint and number conversions aren't
As slow as they used to be .)
Int, uint, and number conversion is not as slow as before.
 
15. Use uint sparingly, it can be slow (Vm has been changing,
From 9 to 10, so int, uint and number conversions aren't as slow
They used to be .)


Use uint conservatively
 
16. Use integers for iterations

For (var I: Int = 0; I <n; I ++)
Not
For (var I: Number = 0; I <n; I ++)
Use int instead of number in the loop
 
18. Multiply vs. Divide: instead of 5000/1000 use: 5000*0.001
Use multiplication * instead of Division/
 
19. Locally store function values in for and while statements instead of repeatedly accessing them

For (..) {A * 180/Math. Pi ;}
Declare: toradians = A * 180/Math. Pi; outside of the loop
Calculate the value to be used in the loop outside the loop and store it in the variable for use in the loop instead of computing in each loop.
 
20. Avoid calculations and method callin Loops

VaR Len: Int = myarray. lengh;
For (VAR I = 0; I
Not:
For (VAR I = 0; I <myarray. lengh; I ++ ){}
Calculate the number of cycles outside the loop (if dynamic) instead of re-computing each loop.
I used to make this mistake -_-!!!
 
21. Use RegEx for validation, use string methods for searching

?


View code
Actionscript3
// postal code validation example using regular expressions
private var regEx:RegExp = /^[A-Z][0-9][A-Z] [0-9][A-Z][0-9]$/i;
private function validatePostal(event :Event):void{ if(regEx.test( zipTextInput.text )) { // handle invalid input case }}// search a string using String methodsvar string : String = "Search me";
var searchIndex : int = string.indexOf("me");
var search: String = string.substring(searchIndex, searchIndex +2);

Use a regular expression to verify the string and use the string substring () method for search.
 
29. Remove unnecessary container wrappers to reduce container nesting

Use minimal container nesting.

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.