Flex常用代码

看来大家对Array情有独钟,对它讨论的很多,这两天看了篇关于flex如何减少代码量减少内存消耗的帖子,其实也像是代码重整,翻译整理了一下。
前面主要是关于Array的,后面涉及很多,不过有些估计不适用于Flex4了。
1. Avoid the new operator when creating Arrays
尽量避免用new操作符来创建数组
var a = [];
NOT:
var a = new 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();

3. Fastest way to copy an array:
复制数组的快捷方法:
var copy : Array = sourceArray.concat();

4. Setting values in Arrays is slow
对数组进行赋值相对慢
employees.push( employee );
employees[2] = employee;

5. Getting values from Arrays is twice as fast as setting
而从数组获取数据相对要快
var employee : Employee = employees[2];

6. Use static for properties methods that do not require an object instance
尽量使用静态属性或方法,避免创建新的对象
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
对不会改变值的属性要设置成常量

public const APPLICATION_PUBLISHER : String = "Company, Inc.";

8. Use final when no subclasses need to be created of a class
没有子类的类应该设置为final
public final class StringUtils

9. Length of method/variable names doesn’t matter in ActionScript 3.0 (true in other langs)
在ActionScript 3.0中方法名/变量名的长度不影响程序运行
someCrazyLongMethodNameDoesntReallyImpactPerformanceTooMuch();

10. One line assignments DO NOT buy any performance (true in other langs)
一行中写多条表达式并不会提高程序性能
var i=0; j=10; k=200;

11. No difference in memory usage between an if statement and a switch statement
if语句和switch语句占用的内存并无差别
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
括住你的if语句,其中应该放经常使用的表达式
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 as

they used to be.)
AVM 在循环计算里将int提升到Number类型了(这样计算会更快)

14. Resolve issues of promotion, unknown, or incorrect object types
尽量使用正确的、明确的对象类型

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;
避免使用uint,它有些慢

16. Use integers for iterations
循环中使用int而不是Number
(var i: int = 0; i < n; i++) NOT for (var i: Number = 0; i < n; i++)

17. Don’t use int with decimals
int类型不接受小数
var decimal : Number = 14.654;
NOT:
var decimal : int = 14.654;

18. Multiply vs. Divide: instead of 5000/1000 use: 5000*0.001
尽量使用乘法代替除法

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

20. Avoid calculations and method calls in loops
避免在循环中进行计算或调用方法
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
使用正则表达式来进行验证字符串,使用String类的方法进行查询
// 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 );

22. Reuse objects to maintain a “memory plateau” DisplayObjects, URLLoader objects
重用对象以减少内存负担

23. Follow the Flex component model:
遵循Flex组件的创建模式:
createChildren(); commitProperties(); updateDisplayList();

24. Only use Datagrids as a last resort (make sure you can’t implement in a regular List first)
避免使用Datagrids

25. Avoid Repeaters for scrollable data
数据很大时避免使用Repeaters

26. Avoid the setStyle() method (One of the most expensive calls in the Flex framework)
避免使用setStyle()方法????

27. Using too many containers dramatically reduces the performance of your application
动态创建太多的容器会降低程序的性能

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:
mxml页面的起始标签不一定是容器

29. Remove unnecessary container wrappers to reduce container nesting
移除无用的组件嵌套

30. Avoid: The VBox container inside an tag, (eliminates redundancy)
避免把Box放到另一个容器(不过,border好像需要)

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 previously created children instead of creating new ones)
使用Repeater时设置recycleChildren为true(但是,组件之间的状态会相互影响)

dataProvider="{repeaterData}">

33. Keep framerate set at 60 fps or lower
设置framerate(帧频率)小于60
<?xml version="1.0" encoding="utf-8"?>

34. Avoid multiple display manipulations per frame
避免每一帧中有太多的现实动作

35. Code against ENTER_FRAME events instead of Timer events
使用ENTER_FRAME 事件代替 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:
使用多页帧时延迟对象的创建

37. Alpha = 0 is not the same as visible = false (Objects marked invisible are passed over)
Alpha = 0 不同于 visible = false
loginButton.visible = false;
NOT:
loginButton.alpha = 0;

原文地址:https://www.cnblogs.com/dzone/p/1987977.html