Flex Optimization Tips Collection [Improve performance]_flex

Source: Internet
Author: User
Tags class definition garbage collection string methods xmlns
1, when creating an array to avoid using the new operator, with var a:array = []; instead of var a:array = new Array ();
2. Quickly copy an array:
var Copy:array = Sourcearray.concat ();
3, set the value of an array is very busy:
Employees.push (employee); EMPLOYEES[2] = employee;
4. The speed of getting a value from an array is twice times faster than setting an array value:
var employee:employee = employees[2];
5, when you do not need a class instance, try to use static properties or methods:
Stringutils.trim ("Text with spaces at end");
Class Definition:package {
Public final class StringUtils {
public static function Trim (s:string): String {
var trimmed:string; Implementation ...
return trimmed;
}
}
}
6. Variables that will not change throughout the life cycle of the program define constants with const:
Public Const Application_publisher:string = "Company, Inc.";
7. When a class does not need to have subclasses, it should declare the class to be of final type:
Public final class StringUtils
8, the length of variables and methods in the AS3 does not affect what performance, but in other languages may have an impact: Somecrazylongmethodnamedoesntreallyimpactperformancetoomuch ();
9. Writing statements on one line does not affect the performance of the AS3 program, but it has an impact in other languages:
var i=0; j=10; k=200;
10, in memory occupies the above if statement and switch statements are not the difference:
Statement:
if (condition) {
Handle condition
}
and statements:
Switch (condition) {
Case "a"://logic to handle case A break;
Case "B"://logic to handle case B-break;
}
Consumes the same amount of memory.
11, when your program processing branches more, you should be appropriate to arrange the order they appear, you can refer to the following way:
if (Conditionthathappensalot) {
Dealing with business logic that happens frequently
else if (conditionthathappenssomtimes) {
Dealing with business logic that happens occasionally
} else {
Dealing with things that are almost never going to happen
}
12, ActionScript virtual machine (Flash Player) recommended in the loop with int instead of number, but Flash Player10 in the Flash Player9 on the basis of a lot of improvements, int, The conversion between uint and number is not as slow as it used to be.
13. Each variable should declare a certain type to resolve the warning message live error message without the specified type.
14, as little as possible with unint, it may be very slow, but Flashplayer10 made improvements, the speed is not as slow as before:
var footerhex:uint = 0X00CCFF;
15, loop traversal time with the int type:
for (var i:int = 0; i < n; i++)
And not:
for (var i:number = 0; i < n; i++)
16. Use number instead of int when using decimal:
var decimal:number = 14.654;
And not:
var decimal:int = 14.654;
17. Use multiplication instead of division:
Replace 100/100 with 100*0.01
18. The calculations used in the for and while loops should be declared well in advance rather than repeated in the loop.
For (..) {A * 180/math. PI; Should be declared on the outside of the loop: Toradians = A*180/math. PI;
19. Avoid calling methods or calculations in loops:
var len:int = Myarray.lengh; for (Var i=0;i<len;i++) {}
And do not use:
for (var i=0;i< myarray.lengh;i++) {}
20, with the regular expression of the string checksum, string of the method to find:
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 methods
var string:string = "Search Me";
var searchindex:int = String.IndexOf ("Me");
var search:string = string.substring (Searchindex, Searchindex + 2);
21, re-use objects such as displayobjects and urlloaderreuse to keep "memory stable".
22. You should follow the Flex component model when using components or creating custom components, such as the method that should be invoked when a component is created.
Createchildren (); Commitproperties (); Updatedisplaylist ();
23, as little as possible with the DataGrid such heavyweight components, unless you use a regular list can not achieve the functionality you want.
24, avoid using repeater control to create scrollable data.
25, try to avoid using the SetStyle () method, this method in the Flex framework is one of the many cost Aogui methods.
26, when you use too much container nesting will lower the performance of the application:
<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>
27, it is not necessary to use the container component every time as your custom component's parent control:
<mx:image xmlns:mx= "Http://www.adobe.com/2006/mxml" source= "avatar.jpg" width = "" "Height =" "/>
28, reduce unnecessary container nesting
29, do not in the panel VBox and Hbox, using the panel's layout properties can be
30, do not use Hbox under the application label, and VBox, truth and 291 kind
31, set Recyclechildren to True to improve repeater performance (reuse the children you have created instead of recreating a new one)
<mx:Script>
<! [cdata[
[bindable]
public var repeaterdata:array = ["Data 1", "Data 2"];
]]>
</mx:Script>
<mx:repeater id= "Repeater" dataprovider= "{repeaterdata}" >
<mx:label Text = "Data item: {Repeater.currentitem}"/>
</mx:Repeater>
32, set the frame rate of the application to 60fps or lower:
<?xml Version = "1.0" encoding= "Utf-8"?>
<mx:application xmlns:mx=http://www.adobe.com/2006/mxml framerate= ">"
</mx:application >
33, to avoid excessive display of each frame operation.
34, can use Enter_frame event without timer event.
Public Function Onenterframe (event:event): void {}
Private Function init (): void {
AddEventListener (Event.enter_frame, onenterframe);
}
And not:
Public Function Ontimertick (event:event): void {}
Private Function init (): void {
var timer:timer = new timer ();
Timer.start ();
Timer.addeventlistener (Timerevent.timer, Ontimertick);
}
35. To delay the creation of objects in multiple frames by the following means.
<mx:container creationpolicy= "queued"/>
36, Alpha = 0 is not visible = False (objects set to invisible will be ignored without any processing)
Loginbutton.visible = false;
And not:
Loginbutton.alpha = 0;
37, performance optimization:
Memory Release Optimization principles
1. All references in the external to the deleted object must be removed cleanly to be disposed of by the system as garbage collection;
2. The child objects inside the parent object are referenced by other external objects, causing the child object to not be deleted and the child object not being deleted, causing the parent object to not be deleted;
3. If an object references an external object, it is important to remember to set this object's reference to NULL when it is deleted or if you do not need to use the reference object;
4. This object can not delete the reason is not necessarily that they have been cited, but also may be their children are outside the reference, the child can not delete the father also deleted;
5. In addition to the need to delete the reference, system components or global tools, management classes, if the uninstall method provided by the need to invoke the deletion of internal objects, otherwise it may cause memory leaks and performance loss;
6. The parent object is deleted immediately, not on behalf of the child object will be deleted or immediately deleted, may be in the late by the system automatically deleted or the second removal of the operation was deleted;
7. If the parent object removes a child object and does not clear a reference to the child object, it cannot be deleted, and the parent object cannot be deleted;
8. Registered Events if not removed does not affect the custom forced collection mechanism, but may affect the normal recycling mechanism, so it is best to do registered event listeners should remember to remove clean.
9. The parent object is deleted does not mean that the rest of the child objects have been deleted, to find a state of the leak code is not equal to other states are not disclosed, to each module of the state of the test and analysis, until the test in any state can delete the entire object.
Memory leaks For example:
1. Reference disclosure: A reference to a child object that needs to be null for external references to this object or to children;
2. System class Leakage: using the system class and forget to do delete operations, such as Bindingutils.bindsetter (), Changewatcher.watch () function after the completion of the call to Changewatcher.unwatch () function to clear the reference, otherwise the object using this function will not be deleted;
Similar to MUSIC, video, IMAGE, TIMER, EVENT, BINDING, etc.
3. Effect leakage: When the application of the effect of the component Effect, when this object is deleted, the object and the Effect animation on the child to stop, and then put the Effect target object null; If you do not stop the animation directly put the Effect null will not be able to remove the object normally.
4. swf disclosure: To completely delete a SWF to invoke its unload () method and set the object null;
5. Picture leakage: When the image object is used, the source should be null; for testing);
6. Sound, video leakage: When you do not need a music or video is needed to stop music, delete objects, reference null;
Memory Leak resolution:
1. Do garbage handling in component Removed_from_stage event (remove all external references (both VO and component need to be removed), remove listener, call System class Cleanup method)
Remove and then reset NULL to ensure that the object removed or RemoveAll after the external reference all released clean;
2. Using Flex's performance optimization tool profile to monitor the project process, you can know what objects history has created, what objects are not being deleted, the number of created, the amount of memory consumed, and the process of creating information;

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.