AS3分享34個flex as3代碼細節性能優化

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章