It seems that everyone has a special liking for Array and has discussed a lot about it. In the past two days, I have read an article about how to reduce flex.CodeThe number of posts that reduce memory consumption is actually like code restructuring. The translation has been sorted out.
Previously, we mainly talked about array, which involves a lot of things later, but some estimates do not apply to flex4.
1. Avoid the new operator when creating Arrays
Try to avoid using the new operator to create an array.
VaR A = [];
Not:
VaR A = new array ();
2. arrays are expensive to create, do so conservatively
It takes a lot to create an array. You must create an array as appropriate.
VaR vanitycollection01: array = new array ();
VaR vanitycollection02: array = new array ();
VaR vanitycollection03: array = new array ();
VaR vanitycollection04: array = new array ();
3. Fastest way to copy an array:
Quick Method for copying Arrays:
VaR copy: array = sourcearray. Concat ();
4. setting values in arrays is slow
Assigning values to arrays is relatively slow.
Employees. Push (employee );
Employees [2] = employee;
5. Getting values from arrays is twice as fast as setting
Retrieving data from arrays is relatively fast.
VaR EMPLOYEE: Employee = employees [2];
6. use static for properties methods that do not require an object instance
Try to use static attributes or methods to avoid creating new objects.
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;
}
}
}
7. Use const for properties that will never change throughout the lifecycle of the application
Set the attribute that does not change the value to a constant.
Public const application_publisher: String = "Company, Inc .";
8. Use final when no subclasses need to be created of A Class
Classes without subclasses should be set to final
Public final class stringutils
9. Length of method/variable names doesn't matter in ActionScript 3.0 (true in other Langs)
The length of the Chinese legal name/variable name in ActionScript 3.0 does not affectProgramRun
Somecrazylongmethodnamedoesntreallyimpactperformancetoomuch ();
10. One line assignments do not buy any performance (true in other Langs)
Writing multiple expressions in a row does not improve program performance.
VaR I = 0; j = 10; k = 200;
11. No difference in memory usage between an if statement and a switch statement
The memory occupied by the if statement and switch statement is no different.
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 ;}
12. Rank your if statements in order of comparisons most likely to be true
Include your if statement, which should contain frequently used expressions
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}
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
They used to be .)
AVM upgraded int to number type in cyclic computing (this will make the calculation faster)
14. resolve issues of promotion, unknown, or incorrect Object Types
Use correct and clear object types whenever possible
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 as they used to be .)
VaR footerhex: uint = 0x00ccff;
Avoid using uint, it is somewhat slow
16. Use integers for iterations
Use int instead of number in the loop
(Var I: Int = 0; I <n; I ++) Not for (var I: Number = 0; I <n; I ++)
17. Don't use int with decimals
Decimal places are not allowed for int type
VaR decimal: Number = 14.654;
Not:
VaR decimal: Int = 14.654;
18. Multiply vs. Divide: instead of 5000/1000 use: 5000*0.001
Use multiplication instead of Division as much as possible
19. Locally store function values in for and while statements instead of repeatedly accessing them
Use variables to store repeated expression values, instead of repeating calculation in a loop.
For (..) {A * 180/Math. Pi;} declare: toradians = A * 180/Math. Pi; outside of the loop
20. Avoid calculations and method callin Loops
Avoid calculating or calling methods in a loop
VaR Len: Int = myarray. lengh; For (VAR I = 0; I
Not:
For (VAR I = 0; I <myarray. lengh; I ++ ){}
21. Use RegEx for validation, use string methods for searching
Use a regular expression to verify the string and use the string class method for query
// 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
String using string Methods var string: String = "Search me"; var searchindex: Int = string. indexof ("me"); var search: String = string. substring (
Searchindex, searchindex + 2 );
22. Reuse objects to maintain a "Memory plateau" displayobjects, urlloader objects
Reuse objects to reduce memory burden
23. Follow the flex component model:
Follow the flex component creation mode:
Createchildren (); commitproperties (); updatedisplaylist ();
24. Only use datagrids as a last resort (make sure you can't implement in a regular list first)
Avoid using datagrids
25. Avoid repeaters for scrollable data
Avoid repeaters when the data is large.
26. Avoid the setstyle () method (one of the most expensive callin the flex Framework)
Avoid using the setstyle () method ????
27. Using too implements containers dramatically CES the performance of your application
Creating too many containers dynamically Reduces Program performance.
TEXT = "label 4"/>
28. You do not need to always use a container tag as the top-level tag of components totally valid component, no top level container needed:
The starting tag of the mxml page is not necessarily a container.
29. Remove unnecessary container wrappers to reduce container nesting
Remove useless component nesting
30. Avoid: The vbox container inside an tag, (eliminates redundancy)
Avoid placing the box in another container (however, border seems to need it)
31. Avoid: vbox container inside an MX: Application tag, (eliminates redundancy)
<? XML version = "1.0" encoding = "UTF-8"?>
/>
Not:
<? XML version = "1.0" encoding = "UTF-8"?>
32. Set the recyclechildren property to true to improve a repeater object's performance (re-uses previusly created children instead of creating new ones)
When repeater is used, set recyclechildren to true (however, the states between components affect each other)
Dataprovider = "{repeaterdata}">
33. Keep framerate set at 60 FPS or lower
Set the framerate (frame frequency) to less than 60
<? XML version = "1.0" encoding = "UTF-8"?>
34. Avoid Multiple display manipulations per frame
Avoid too many real actions in each frame.
35. Code against enter_frame events instead of timer events
Use enter_frame event instead of timer events
Public Function onenterframe (Event: Event): void {} private function Init (): void {addeventlistener (event. enter_frame, onenterframe );}
Not:
Public Function ontimertick (Event: Event): void {} private function Init (): void {var Timer: timer = new timer (); timer. Start ();
Timer. addeventlistener (timerevent. Timer, ontimertick );}
36. To defer object creation over multiple frames use:
Delay object creation when multiple pages are used
37. Alpha = 0 is not the same as visible = false (Objects marked invisible are passed over)
Alpha = 0 is different from visible = false
Loginbutton. Visible = false;
Not:
Loginbutton. Alpha = 0;