Flex/AS优化细节

自己翻译的,原文见此:
http://insideria.com/2009/04/51-actionscript-30-and-flex-op.html

[color=blue]1.在创建新的Array的时候尽量不要使用new[/color]
使用
var a = []; 

而不是
var a = new Array();  


[color=blue]2.创建Array非常消耗资源,建议保守使用[/color]

[color=blue]3.复制Array最快的方法是:[/color]
var copy : Array = sourceArray.concat();    


[color=blue]4.对Array中元素赋值是很慢的[/color]
employees.push( employee ); 
employees[2] = employee;


[color=blue]5.取得Array中元素的值比赋值快一倍[/color]
var employee : Employee = employees[2];


[color=blue]6.将不需要对象实例的方法设置为静态(static)[/color]

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;
}
}
}


[color=blue]7.将整个应用生命周期内都不会改变的属性设置为常量(const)[/color]

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

[color=blue]
8.当一个类不会再有子类时,使用final关键字[/color]

public final class StringUtils 


[color=blue]9.方法/变量/常量的名字长度不会影响性能(在其它语言中往往并非如此)[/color]

someCrazyLongMethodNameDoesntReallyImpactPerformanceTooMuch();

[color=blue]
10.将一些元素在同一行中赋值不会提升性能(在其它语言中往往并非如此)[/color]

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

[color=blue]
11.在内存占用上,if和switch几乎没有区别[/color]

if ( condition )
{
// handle condition
}

同下边的代码占用相近的内存
 
switch ( condition )
{
case "A":
// logic to handle case A
break;

case "B":
// logic to handle case B
break;
}

[color=blue]12.对if的条件进行排序,最经常遇到的情况放在前面[/color]
 
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
}


[color=blue]13. AVM在循环中会将int转换为Number,不过AVM也在变化,现在int/uint向Number的转换没有之前那么慢了[/color]

[color=blue]14.正确处理未知、不正确的Object类型极其转换[/color]

[color=blue]15.保守使用uint,因为它非常慢,不过VM也在改进,对uint的处理速度也在不断提升[/color]
 
var footerHex : uint = 0x00ccff;


[color=blue]16. 在循环条件中使用int[/color]
使用
 
(var i: int = 0; i < n; i++)

而不是
 
for (var i: Number = 0; i < n; i++)


[color=blue]17.不要把浮点数赋给int变量[/color]

var decimal : Number = 14.654;

而不是

var decimal : int = 14.654;


[color=blue]18.尽量使用乘法运算代替除法运算[/color]

如:使用5000*0.001来代替5000/1000

[color=blue]19.循环体中经常用到的函数或计算可放在循环体外[/color]

使用

for (..){ a * 180 / Math.PI; }

不如在外边声明
toRadians = a*180/Math.PI;


[color=blue]20.在循环中避免计算或者调用函数[/color]

最典型的应用莫过于使用:
 
var len : int = myArray.lengh;
for (var i=0;i<len;i++){}

而不是:
  
for (var i=0;i< myArray.lengh;i++){ }

[color=blue]
21.使用RegEx来进行验证,使用string的方法进行查找[/color]

 
// 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 );


[color=blue]22. 重用诸如 DisplayObjects, URLLoader 一类的对象来维持“内存平稳”[/color]

[color=blue]23. 遵循Flex组件模型:[/color]
  
createChildren();
commitProperties();
updateDisplayList();

[color=blue]
24. 在没有其它办法的情况下再使用DataGrid。你确信它不能用List实现吗?

25. 避免对可串行查看的数据使用Repeater

26. 避免使用setStyle()函数,它是Flex框架中最消耗资源的函数之一

27. 使用过多的容器将会极大影响程序的性能[/color]
 
<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>


[color=blue]28. 没必要一直保有一个最顶层容器,直接使用标签也是完全可以的[/color],如
 
<mx:Image xmlns:mx="http://www.adobe.com/2006/mxml"
source="avatar.jpg" width="200" height="200" />

[color=blue]
29. 去掉不需要的容器以减少容器嵌套

30. 去掉容器中VBox(减少冗余)[/color]
  
<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>


[color=blue]31.避免在mx:Application标签中使用VBox(减少冗余)[/color]

使用:
  
<?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:VBox>
<mx:Label text="Label 1" />
<mx:Label text="Label 2" />
</mx:VBox>
</mx:Application>


[color=blue]32. 设置Repeater的recycleChildren属性为true以提升性能[/color]
 
<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>


[color=blue]33. 帧频设置为60fps以下[/color]

 
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx=http://www.adobe.com/2006/mxml
frameRate="45">
</mx:Application>


[color=blue]34. 避免每帧同时改变多个显示对象

35. 尽量使用ENTER_FRAME事件而不是Timer事件[/color]
 
public function onEnterFrame( event : Event ) : void
{
}
private function init() : void
{
addEventListener( Event.ENTER_FRAME, onEnterFrame );
}

而不是:
 
public function onTimerTick( event : Event ) : void
{
}
private function init() : void
{
var timer : Timer = new Timer();
timer.start();
timer.addEventListener( TimerEvent.TIMER, onTimerTick );
}


[color=blue]36. 推迟具有多个帧时对象的建立[/color]
 
<mx:Container creationPolicy="queued"/>

[color=blue]
37. Alpha = 0 不等同于 visible = false
visible置false的对象被从显示列表中移除了 [/color]

使用
 
loginButton.visible = false;

而不是
 
loginButton.alpha = 0;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值