Performance optimization for ActionScript 3 and flex frameworks

Source: Internet
Author: User
Tags class definition

A few days ago by the boss recommended an article, from 7yue Brother's blog, the author is Sean Moore,

Original address: http://www.insideria.com/2009/04/51-actionscript-30-and-flex-op.html

Instead of being bloated after the program has been written, it is better to keep an eye on the key points and remind yourself at all times. Translate it out so that you can check it out later.

1 Avoid using its constructor when creating a new array.

Do this: var a = [];

Instead of doing this: var a = new Array ();

2 Creating an array is a very consuming operation, so be careful with the following types of actions:

var vanitycollection01:array = new Array ();

var vanitycollection02:array = new Array ();

var vanitycollection03:array = new Array ();

var vanitycollection04:array = new Array ();

3 The quickest way to copy an array is:

var Copy:array = Sourcearray.concat ();

4 regardless of which method you use, setting the value of an array element is a slow operation.

Employees.push (employee);

EMPLOYEES[2] = employee;

5 the speed at which a value is obtained in an array is twice times the value set.

var employee:employee = employees[2];

6 Set the property function to a static function so that you do not have to instantiate an object of that class when you use it.

Stringutils.trim ("Text with spaces at end");

Class definition:

Package

{

Public final class StringUtils

{

public static function Trim (s:string): String

{

var trimmed:string;

Logical implementation Code

return trimmed;

}

}

}

7 Use the constant keyword const to define attributes that do not change value during the running of the program.

Public Const Application_publisher:string = "Company, Inc.";

8 When a class no longer requires a subclass, it is defined as the final class.

Public final class StringUtils

9 long function names and variable names do not cause any additional consumption in Action Script 3 (also in other languages)

Somecrazylongmethodnamedoesntreallyimpactperformancetoomuch ();

10 defining multiple variables within a single line does not create any performance improvement (also in other languages)

var i=0; j=10; k=200;

11 There is no difference between using if and using switch to make logical judgments about the memory consumed, for example:

if (condition)

{

The logic under the processing condition

}

Follow the use of switch

Switch (condition)

{

Case "A":

The processing logic under a condition

Break

Case "B":

The processing logic under B condition

Break

}

There is no difference in memory consumption.

12 use if to make logical judgments, arrange as much as possible in the order in which they are most likely to occur. For example:

if (most likely to happen)

{

Handle what is most likely to happen.

}

else if (sometimes the case happens)

{

Dealing with situations that can happen sometimes.

}

Else

{

Handling the situation when none of the above judgments occurred.

}

When calculating within the loop body, the integer int data is promoted to floating-point number for processing (from FP9 to FP10, the virtual machine has changed, and the conversion between int,uint,number is not as slow as before). )

14 note the problem of resolving type conversions, unknown types (unknown), illegal types (incorrect).

15 use UINT carefully, it slows down the program.

var footerhex:uint = 0X00CCFF;

16 using integers as growth in iterators

You should use this:

for (var i:int = 0; i < n; i++)

Instead of:

for (var i:number = 0; i < n; i++)

17 Do not assign a decimal value to an int type variable.

This should be done in this way:

var decimal:number = 14.654;

No:

var decimal:int = 14.654;

18 Multiplication vs Division: Use 5000*0.001 to replace 5000/1000.

19 If you want to use a value frequently in a for or while loop, use a local variable to store it instead of frequently calculating it.

With such frequent computations it:

For (..) {A * 180/MATH.PI;}

Instead, define a variable to store it:

var toradians:number = A*180/math.pi;

20 avoid calculations in the condition of the circulation body, for example:

var len:int = Myarray.lengh;

for (Var i=0;i<len;i++) {}

And don't do this:

for (var i=0;i< myarray.lengh;i++) {} (by. That's what I've been doing. )

21 Use regular expressions for string checking and string searches using String functions.

For example: using regular expressions to do zip code test

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 the situation where the input format is satisfied

}

}

To work with string queries using String functions:

var string:string = "Search Me";

var searchindex:int = String.IndexOf ("Me");

var search:string = string.substring (Searchindex, Searchindex + 2);

22 Try to reuse objects that are part of the memory high consumption area, such as Displayobjects,urlloader.

23 Draw on the design pattern of the Flex object:

Createchildren ();

Commitproperties ();

Updatedisplaylist ();

24 Use the DataGrids component as your last display tool (if you're sure you really don't have the ability to use a regular list to achieve what you want, use it)

25 Avoid using iterator iterations for data with scrolling capabilities.

26 Avoid using the SetStyle () function (which is one of the most performance-consuming behaviors in the flex framework)

27 using too many container nesting is bound to degrade the performance of your program. For example, here's this disgusting nesting.

<mx:Panel>

<mx:VBox>

<mx:HBox>

<mx:label text= "Label 1"/>

<mx:VBox>

<mx:label text= "Label 2"/>

</mx:VBox>

<mx:HBox>

<mx:label text= "Label 3"/>

<mx:VBox>

<mx:label text= "Label 4"/>

</mx:VBox>

</mx:HBox>

</mx:HBox>

</mx:VBox>

</mx:Panel>

28 You don't have to label each container with a namespace, only the top-level container needs to do this. The following is not necessary.

<mx:image xmlns:mx= "Http://www.adobe.com/2006/mxml"

Source= "Avatar.jpg" width= "height="/>

29 Remove unnecessary containers to reduce nesting of containers.

30 Avoid nested vbox containers in the label (Eliminate redundancy)

<mx:Panel>

<mx:label text= "Label 1"/>

<mx:label text= "Label 2"/>

</mx:Panel>

<mx:Panel>

<mx:VBox>

<mx:label text= "Label 1"/>

<mx:label text= "Label 2"/>

</mx:VBox>

</mx:Panel>

31 Avoid using the VBox label inside the mx:application label. (Eliminates redundancy)

<?xml version= "1.0" encoding= "Utf-8"?>

<mx:application xmlns:mx=http://www.adobe.com/2006/mxml>

<mx:label text= "Label 1"/>

<mx:label text= "Label 2"/>

</mx:Application>

And not:

<?xml version= "1.0" encoding= "Utf-8"?>

<mx:application xmlns:mx=http://www.adobe.com/2006/mxml>

<mx:VBox>

<mx:label text= "Label 1"/>

<mx:label text= "Label 2"/>

</mx:VBox>

</mx:Application>

32 Setting Repeater's Recyclechildren property to True can elevate its performance (using previously created objects instead of creating a new object)

<mx:Script>

<! [cdata[

[bindable]

public var repeaterdata:array = ["Data 1", "Data 2"];

]]>

</mx:Script>

<mx:repeater id= "Repeater" dataprovider= "{repeaterdata}" recyclechildren= "true" >

<mx:label text= "Data item: {Repeater.currentitem}"/>

</mx:Repeater>

33 Set the frame frequency (framerate) to 60 or lower.

<?xml version= "1.0" encoding= "Utf-8"?>

<mx:application Xmlns:mx=http://www.adobe.com/2006/mxml

Framerate= ">"

</mx:Application>

34 Avoid processing multiple display objects within each frame.

35 Replace the timer event with the Enter_frame event

Use:

Public Function Onenterframe (event:event): void

{

}

Private Function init (): void

{

AddEventListener (Event.enter_frame, onenterframe);

}

And do not use:

Public Function Ontimertick (event:event): void

{

}

Private Function init (): void

{

var timer:timer = new timer ();

Timer.start ();

Timer.addeventlistener (Timerevent.timer, Ontimertick);

}

36 Use the Display object in multiple frames, postpone its object creation by using the following methods:

<mx:container creationpolicy= "queued"/>

Alpha = 0 is not equal to Visible = False (the object will not be processed when it is not visible)

So, use:

Loginbutton.visible = false;

Instead of:

Loginbutton.alpha = 0;

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.