ActionScript 3 和 Flex架構的效能最佳化

來源:互聯網
上載者:User

幾日前被老總推薦文章一篇,來自7yue兄的blog,作者是Sean Moore ,

原文地址:http://www.insideria.com/2009/04/51-actionscript-30-and-flex-op.html

與其在程式寫完了之後臃腫得跑不動,不如平時注意這些關鍵點,時時提醒自己。翻譯出來,以便以後時時查閱。

 

1 建立新數組時避免使用它的建構函式。

 

   這樣做:var a = []; 

 

   而不要這樣做:var a = new Array();

 

2 建立數組是一個消耗量很大的操作,所以請謹慎進行以下類型的操作:

 

  var vanityCollection01 : Array = new Array(); 

  var vanityCollection02 : Array = new Array(); 

  var vanityCollection03 : Array = new Array(); 

  var vanityCollection04 : Array = new Array();

3 複製一個數組最快的方式是:

  

  var copy : Array = sourceArray.concat(); 

4 無論你用哪種方式,為數組的元素設定值都是一個慢的操作。

  employees.push( employee ); 

  employees[2] = employee; 

5 在數組中獲得一個值的速度是設定一個值的二倍。

  var employee : Employee = employees[2];

6 將屬性函數設定為靜態函數,這樣你在使用它的時候就不用執行個體化一個該類的對象。

  StringUtils.trim( "text with space at end " ); 

  類定義: 

  package 

  { 

     public final class StringUtils 

         { 

          public static function trim( s : String ) : String 

          { 

               var trimmed : String; 

               // 邏輯實現代碼 

               return trimmed; 

           } 

      } 

  } 

7 使用常量關鍵字const來定義那些在程式運行周期內都不會發生值改變的屬性。

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

8 當一個類不再需要有子類的時候,將它定義為final類。

  public final class StringUtils

9 巨長的函數名和變數名在Action Script 3中不會造成任何額外的消耗,(在其他語言中也是)

  someCrazyLongMethodNameDoesntReallyImpactPerformanceTooMuch();

10 在單行內定義多個變數不會帶來任何效能的提升(在其他語言中也是)

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

11 使用if和使用switch做邏輯判斷所消耗的記憶體是沒有區別的,例如:

   if ( condition ) 

   { 

     // 處理條件下的邏輯

   } 

   跟使用switch

   switch ( condition ) 

   { 

     case "A": 

         // A條件下的處理邏輯

     break; 

      

     case "B": 

         // B條件下的處理邏輯  

     break; 

   }

   沒有任何記憶體消耗上的區別。

12 使用if做邏輯判斷時,儘可能的按照最有可能發生的情況的順序來順序排列。例如:

if ( 最有可能發生的情況 ) 

     // 處理最有可能發生的情況。

else if ( 有時候會發生的情況 )  

     // 處理有時候會發生的情況。

else  

     // 處理以上判斷都沒有發生時的情況 。

13 AVM在迴圈體內部進行計算時,將整型int資料提升為浮點型Number進行處理,(從fp9到fp10,虛擬機器已經有所改變,int,uint,number之間的轉換不再像之前那麼慢了。)

14 注意解決類型轉換,未知類型(unknown),非法類型(incorrect)的問題。

15 謹慎使用 uint ,它會使程式變慢。

   var footerHex : uint = 0x00ccff;

16 在迭代器中使用整型作為增長量

   應該這樣使用:

   for(var i: int = 0; i < n; i++) 

   而不是:

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

17 不要為int型變數賦小數值。

   應該這樣用:

   var decimal : Number  = 14.654; 

   不應該:

   var decimal : int  = 14.654; 

18 乘法 vs 除法:使用 5000*0.001 來替代 5000/1000。

19 如果你要在for或者while迴圈體內頻繁的使用一個值,請使用一個本地變數來存放它,而不是去頻繁的計算它。

   與其這樣頻繁的計算它:

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

   不如定義一個變數來存放它:  

   var toRadians:Number = a*180/Math.PI;

20 避免在迴圈體判斷條件中進行計算,例如:

   var len : int = myArray.lengh;  

   for (var i=0;i<len;i++){} 

   而不要這樣做:

   for (var i=0;i< myArray.lengh;i++){ } (靠。我一直都這麼乾的。)

21 使用Regex來進行字串檢查,並使用字串函數來進行字串搜尋。

   例如:使用Regex做郵遞區號檢驗

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

     { 

          // 處理輸入格式滿足的情況

      } 

   }

   使用字串函數處理字串查詢:

   var string : String = "Search me"; 

   var searchIndex : int = string.indexOf( "me" ); 

   var search : String = string.substring( searchIndex, searchIndex + 2 ); 

22 盡量重複使用那些屬於“記憶體高消耗區”的對象,例如,DisplayObjects,URLLoader。

23 借鑒Flex對象的設計模式:

   createChildren(); 

   commitProperties(); 

   updateDisplayList(); 

24 把使用Datagrids組件作為你最後的顯示手段(如果你確信你真的沒有辦法使用一個常規的list實現你想要的功能,才使用它)

25 避免使用迭代器迭代具備滾動功能的資料。

26 避免使用setStyle()函數(這在Flex架構裡是效能消耗量最大的行為之一)

27 使用過多的容器嵌套勢必會降低你程式的效能。例如下面這個噁心的嵌套。

<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 你不用為每個容器都加上命名空間的標籤,只有頂級容器需要這樣做。下面這個就是不必要的。

   <mx:Image xmlns:mx="http://www.adobe.com/2006/mxml"  

     source="avatar.jpg" width="200" height="200" />

29 移除不必要的容器來減少容器嵌套。

30 避免在標籤內嵌套VBox容器(消除冗餘)

<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標籤。(消除冗餘)

 

<?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>

 

32 設定Repeater的recycleChildren屬性為true可以提升它的效能(使用之前建立過的對象,而不是建立一個新對象)

<mx:Script> 

    <![CDATA[ 

        [Bindable] 

        public var repeaterData : Array = ["data 1", "data 2"]; 

    ]]> 

</mx:Script> 

 

<mx:Repeater id="repeater" dataProvider="{repeaterData}" recycleChildren="true">  

    <mx:Label text="data item: {repeater.currentItem}"/> 

</mx:Repeater> 

33 將幀頻(framerate)設定為60或者更低。

 

<?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事件

使用:

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

36 在多幀中使用顯示對象時,使用以下方法延遲它的對象建立:

 

<mx:Container creationPolicy="queued"/>  

37 alpha = 0 並不等同於 visible = false(對象在不可見時將會不會被處理)

所以,使用:

loginButton.visible = false;

而不是:

loginButton.alpha = 0;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.