1.在创建新的Array的时候尽量不要使用new
使用
Actionscript代码
var a = [];??
var a = [];
而不是
Actionscript代码
var a = new Array();???
var a = new Array();?
2.创建Array非常消耗资源,建议保守使用
3.复制Array最快的方法是:
Actionscript代码
var copy : Array = sourceArray.concat();?????
var copy : Array = sourceArray.concat();???
4.对Array中元素赋值是很慢的
Actionscript代码
employees.push( employee );???
employees[2] = employee;?
employees.push( employee );
employees[2] = employee;
5.取得Array中元素的值比赋值快一倍
Actionscript代码
var employee : Employee = employees[2];?
var employee : Employee = employees[2];
6.将不需要对象实例的方法设置为静态(static)
Actionscript代码
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;???
?????????? }???
????? }???
}??
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.将整个应用生命周期内都不会改变的属性设置为常量(const)
Actionscript代码
public const APPLICATION_PUBLISHER : String = “Company,Inc.”;??
public const APPLICATION_PUBLISHER : String = “Company,Inc.”;
8.当一个类不会再有子类时,使用final关键字
Actionscript代码
public final class StringUtils??
public final class StringUtils
9.方法/变量/常量的名字长度不会影响性能(在其它语言中往往并非如此)
Actionscript代码
someCrazyLongMethodNameDoesntReallyImpactPerformanceTooMuch();?
someCrazyLongMethodNameDoesntReallyImpactPerformanceTooMuch();
10.将一些元素在同一行中赋值不会提升性能(在其它语言中往往并非如此)
Actionscript代码
var i=0; j=10; k=200;??
var i=0; j=10; k=200;
11.在内存占用上,if和switch几乎没有区别
Actionscript代码
if ( condition )???
{???
???? // handle condition???
}??
if ( condition )
{
???? // handle condition
}
同下边的代码占用相近的内存
Actionscript代码
???
switch ( condition )???
{???
???? case “A”:???
???????? // logic to handle case A???
???? break;???
????????
???? case “B”:???
???????? // logic to handle case B????
???? break;???
}??
?
switch ( condition )
{
???? case “A”:
???????? // logic to handle case A
???? break;
?????
???? case “B”:
???????? // logic to handle case B?
???? break;
}
12.对if的条件进行排序,最经常遇到的情况放在前面
Actionscript代码
???
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???
}??
?
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在循环中会将int转换为Number,不过AVM也在变化,现在int/uint向Number的转换没有之前那么慢了
14.正确处理未知、不正确的Object类型极其转换
15.保守使用uint,因为它非常慢,不过VM也在改进,对uint的处理速度也在不断提升
Actionscript代码
???
var footerHex : uint = 0x00ccff;??
?
var footerHex : uint = 0x00ccff;
16. 在循环条件中使用int
使用
Actionscript代码
???
(var i: int = 0; i < n; i++)?
?
(var i: int = 0; i < n; i++)
而不是
Actionscript代码
???
for (var i: Number = 0; i < n; i++)??
?
for (var i: Number = 0; i < n; i++)
17.不要把浮点数赋给int变量
Actionscript代码
var decimal : Number? = 14.654;??
var decimal : Number? = 14.654;
而不是
Actionscript代码
var decimal : int? = 14.654;??
var decimal : int? = 14.654;
18.尽量使用乘法运算代替除法运算
如:使用5000*0.001来代替5000/1000
19.循环体中经常用到的函数或计算可放在循环体外
使用
Actionscript代码
for (..){ a * 180 / Math.PI; }??
for (..){ a * 180 / Math.PI; }
不如在外边声明
Actionscript代码
toRadians = a*180/Math.PI;?
toRadians = a*180/Math.PI;
20.在循环中避免计算或者调用函数
最典型的应用莫过于使用:
Actionscript代码
???
var len : int = myArray.lengh;????
for (var i=0;i<len;i++){}???
??
?
var len : int = myArray.lengh;?
for (var i=0;i<len;i++){}
?
而不是:
Actionscript代码
????
for (var i=0;i< myArray.lengh;i++){ }?
?
for (var i=0;i< myArray.lengh;i++){ }
21.使用RegEx来进行验证,使用string的方法进行查找
Actionscript代码
???
// 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 );??
?
// 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. 重用诸如 DisplayObjects,URLLoader 一类的对象来维持“内存平稳”
23. 遵循Flex组件模型:
Actionscript代码
????
createChildren();???
commitProperties();???
updateDisplayList();??
?
createChildren();
commitProperties();
updateDisplayList();
24. 在没有其它办法的情况下再使用DataGrid。你确信它不能用List实现吗?
25. 避免对可串行查看的数据使用Repeater
26. 避免使用setStyle()函数,它是Flex框架中最消耗资源的函数之一
27. 使用过多的容器将会极大影响程序的性能
Mxml代码
???
<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>??
?
<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. 没必要一直保有一个最顶层容器,直接使用标签也是完全可以的,如
Mxml代码
???
<mx:Image xmlns:mx=”http://www.adobe.com/2006/mxml“????
???? source=”avatar.jpg” width=”200″ height=”200″ />??
?
<mx:Image xmlns:mx=”http://www.adobe.com/2006/mxml“?
???? source=”avatar.jpg” width=”200″ height=”200″ />
29. 去掉不需要的容器以减少容器嵌套
30. 去掉容器中VBox(减少冗余)
Mxml代码
????
<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>??
?
<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.避免在mx:Application标签中使用VBox(减少冗余)
使用:
Mxml代码
????
<?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>??
?
<?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>
而不是:
Mxml代码
<?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>??
<?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. 设置Repeater的recycleChildren属性为true以提升性能
Mxml代码
???
<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>??
?
<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>
33. 帧频设置为60fps以下
Mxml代码
???
<?xml version=”1.0″ encoding=”utf-8″?>???
<mx:Application xmlns:mx=http://www.adobe.com/2006/mxml????
??? frameRate=”45″>???
</mx:Application>??
?
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=http://www.adobe.com/2006/mxml?
??? frameRate=”45″>
</mx:Application>
34. 避免每帧同时改变多个显示对象
35. 尽量使用ENTER_FRAME事件而不是Timer事件
Actionscript代码
???
public function onEnterFrame( event : Event ) : void???
{???
}???
private function init() : void???
{???
???? addEventListener( Event.ENTER_FRAME,onEnterFrame );???
}??
?
public function onEnterFrame( event : Event ) : void
{
}
private function init() : void
{
???? addEventListener( Event.ENTER_FRAME,onEnterFrame );
}
而不是:
Actionscript代码
???
public function onTimerTick( event : Event ) : void???
{???
}???
private function init() : void???
{???
???? var timer : Timer = new Timer();???
???? timer.start();???
???? timer.addEventListener( TimerEvent.TIMER,onTimerTick );???
}??
?
public function onTimerTick( event : Event ) : void
{
}
private function init() : void
{
???? var timer : Timer = new Timer();
???? timer.start();
???? timer.addEventListener( TimerEvent.TIMER,onTimerTick );
}
36. 推迟具有多个帧时对象的建立
Actionscript代码
???
<mx:Container creationPolicy=”queued”/>???
?
<mx:Container creationPolicy=”queued”/>?
37. Alpha = 0 不等同于 visible = false
visible置false的对象被从显示列表中移除了
使用
Actionscript代码
???
loginButton.visible = false;??
?
loginButton.visible = false;
而不是
Actionscript代码
???
loginButton.alpha = 0;
原文:http://insideria.com/2009/04/51-actionscript-30-and-flex-op.html