`
hehailin1986_163.com
  • 浏览: 153954 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Round up of ActionScript 3.0 and Flex optimization techniques and practices

阅读更多

http://insideria.com/2009/04/51-actionscript-30-and-flex-op.html

 

A homework assignment I was recently given for a Java programming class involved a competition to see who could create the most optimized implementation of an interface which was provided by the instructor. It was a challenging and very fun assignment that I think the whole class enjoyed. I didn’t win the competition but still came out a winner because of my heightened interest in application optimization and performance tuning that I gained.

I’m personally a pretty big fan of coding standards and have been ribbed by many developers over some of the longer method, variable and class names that I sometimes choose. I've always leaned toward the side of programming that employs standards and frameworks . Rather than spending a ton of time digging around in compiler specs and messing with GC (Garbage Collection) for reasons of performance, tuning and optimization. I was leaving this to the seasoned programmers creating the standards and frameworks I use.

This isn’t to say I’ve never paid attention to performance and I enjoy building slow applications. It’s almost like two different worlds; the optimization world and the standards world. They don’t always agree with each other. There can sometimes be a trade off for performance over readability and organization or vice-versa. This article is meant to stand next to the Flex Best Practices articles that I authored.

While creating my concrete implementation for the homework assignment I discovered a powerful profiling engine in NetBeans. The NetBeans profiling engine helped me understand some of the memory usage and consumption of each property, method call and object instantiation in my program. This profiler in NetBeans is very similar to the one found in Flex Builder. Both are very powerful and very useful. I've been exploring the Flex Profiler in greater detail lately as well and using it to eradicate memory leaks for a real world application I’ve been refactoring to best practices lately.

The Java optimization homework has increased my interest in optimization and profiling for ActionScript 3.0 and Flex development. I've been piecing together ActionScript optimization techniques and practices from around the web for a couple years now. Some of these techniques are in opposition to what the standards dictate but most of software development is this way. You have to learn when to use some techniques and when to leave some out.

Here is a round up of ActionScript 3.0 and Flex optimization techniques and practices. I’ve scoured the web for and filtered practices and techniques that can be adopted into your application development process.

1. Avoid the new operator when creating Arrays

 
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

 
public final class StringUtils 

9. Length of method/variable names doesn't matter in ActionScript 3.0 (true in other langs)

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

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; 

16. Use integers for iterations

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

17. Don't use int with decimals

 
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<len;i++){} 

NOT:

  
for (var i=0;i< myArray.lengh;i++){ } 

21. Use RegEx for validation, use string methods for searching

 
// 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:

 
createChildren(); 
commitProperties(); 
updateDisplayList(); 

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

25. Avoid Repeaters for scrollable data

26. Avoid the setStyle() method (One of the most expensive calls in the Flex framework)

27. Using too many containers dramatically reduces the performance of your application

 
<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. 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:

 
<mx:Image xmlns:mx="http://www.adobe.com/2006/mxml"  
     source="avatar.jpg" width="200" height="200" /> 

29. Remove unnecessary container wrappers to reduce container nesting

30. Avoid: The VBox container inside an tag, (eliminates redundancy)

 
<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. Avoid: VBox container inside an mx:Application tag, (eliminates redundancy)

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

NOT:

 
<?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. Set the recycleChildren property to true to improve a Repeater object's performance (re-uses previously created children instead of creating new ones)

 
<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. Keep framerate set at 60 fps or lower

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

34. Avoid multiple display manipulations per frame

35. Code against ENTER_FRAME events instead of 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:

 
<mx:Container creationPolicy="queued"/>  

37. Alpha = 0 is not the same as visible = false (Objects marked invisible are passed over)

 
loginButton.visible = false; 

NOT:

 
loginButton.alpha = 0; 

分享到:
评论

相关推荐

    Flex第一步--基于ActionScript 3.0的Flex 2应用开发 第2、 6、 13章

    本资料主要聚焦在Flex 2的应用开发,特别是结合ActionScript 3.0这一强大的编程语言进行的实践操作。ActionScript 3.0是Flash Platform的核心编程语言,它具有更严格的类型系统、更高的性能和更多的面向对象特性。 ...

    DDP.rar_ActionScript3.0_flex

    ActionScript3.0和Flex是Adobe开发的两个关键技术,用于创建富互联网应用程序(RIA)。在本项目"DDP.rar_ActionScript3.0_flex"中,我们主要关注的是一个使用这些技术实现的对对碰小游戏。 ActionScript3.0是Flash ...

    Foundation ActionScript 3.0 with Flash CS3 and Flex.2008.pdf

    《Foundation ActionScript 3.0 with Flash CS3 and Flex》一书由Steve Webster、Todd Yard和Sean McSharry合著,旨在为读者提供一个全面深入理解ActionScript 3.0编程语言及其在Adobe Flash CS3和Adobe Flex环境中...

    ActionScript 3.0 API文档(CHM+HTML最新中文完整)

    《ActionScript 3.0 语言和组件参考》概述 《ActionScript 3.0 语言参考》是 Flash® Player 和 Adobe AIR™ 应用程序编程接口 (API) 的参考手册。 此版本包括支持 Adobe AIR 运行时环境的新 ActionScript 类、...

    ActionScript 3.0开发技术大全(第一部分)

    第1篇 ActionScript3.0语言基础  第1章 ActionScript3.0概述  第2章 搭建ActionScript3.0开发环境  第3章 ActionScript3.0语法 第2篇 ActionScript3.0面向对象特性  第4章 ActionScript3.0面向对象编程  第...

    Foundation ActionScript 3.0 with Flash CS3 and Flex.pdf

    - **书名**:《Foundation ActionScript 3.0 with Flash CS3 and Flex》 - **作者**:Steve Webster, Todd Yard 和 Sean McSharry - **出版年份**:2008年 - **ISBN**:978-1-59059-815-3(平装版)、978-1-4302-...

    《Flex第一步_基于ActionScript 3.0的Flex 2应用开发

    《Flex第一步:基于ActionScript 3.0的Flex 2应用开发》这本书是为初学者设计的,旨在帮助读者快速掌握Adobe Flex 2框架的使用,特别是与ActionScript 3.0的集成。Flex是一个开源的开发平台,主要用于构建富互联网...

    ActionScript 3.0 编程.rar

    Flash Builder是一款强大的集成开发环境(IDE),专为ActionScript 3.0和Flex应用程序设计。它提供了代码提示、调试工具、自动完成等功能,极大地方便了开发过程。通过Flash Builder,开发者可以创建、测试和部署RIA...

    Flash ActionScript3.0教程示例(从入门到精通).rar

    《Flash ActionScript3.0教程示例(从入门到精通)》是一本全面解析ActionScript3.0编程语言在Flash平台上的应用的教程。ActionScript3.0是Adobe Flash Professional中的核心编程语言,用于创建交互式动画、游戏、富...

    Foundation ActionScript3.0 with Flash CS3 and Flex

    《Foundation ActionScript3.0 with Flash CS3 and Flex》是一本深入探讨ActionScript3.0在Adobe Flash CS3和Flex中的应用的专业书籍。本书由Steve Webster、Todd Yard和Sean McSharry共同撰写,三位作者都是在Flash...

    Foundation ActionScript 3.0 with Flash CS3 and Flex

    Use the fundamentals of ActionScript 3.0 with both the Flash IDE and Flex Take advantage of ActionScript 3.0's object-oriented features Manipulate sound and video to produce exciting modern web ...

    flex中文帮助 ActionScript3.0中文帮助

    ActionScript 3.0 语言和组件参考概述Adobe Flex 2 语言参考ActionScript 3.0 语言和组件参考是适用于 Flash® Player 应用程序编程接口 (API) 的参考手册。 Adobe Flex 2 语言参考ActionScript 3.0 语言和组件...

    ActionScript3.0 Game Programing中文版

    《ActionScript3.0 Game Programming》是一本专为游戏开发者设计的权威教程,中文版的出版使得更多中国读者能够深入理解和掌握ActionScript3.0在游戏开发中的应用。这本书不仅覆盖了ActionScript的基础语法和面向...

    ActionScript3.0宝典 pdf 高清晰

    ActionScript 3.0 是Adobe开发的一种编程语言,主要用于创建交互式内容,特别是在Flash和Flex平台上。这个“ActionScript 3.0 宝典”很可能是一本全面介绍该语言的教程,适合初学者和有一定经验的开发者。PDF版本...

    actionscript 3.0动画制作教程

    ActionScript 3.0是Adobe Flash Professional、Flash Player以及Adobe AIR中的主要编程语言,用于创建交互式内容、游戏和富互联网应用程序(RIA)。本教程针对ActionScript 3.0的动画制作进行了深入讲解,旨在帮助...

    ActionScript3.0完全自学手册电子教案_ActionScript3.0_

    ActionScript3.0是Adobe开发的一种面向对象的编程语言,主要用于创建富互联网应用程序(RIA),尤其是在Flash平台上。它是Flash Professional、Flash Builder等工具的核心组成部分,让开发者能够为网页、桌面和移动...

    Flash ActionScript 3.0 编程技术教程(附书光盘)

    本书全面介绍了ActionScript 3.0的基本语法、类的架构以及面向对象程序的设计理念。内容安排上既适合刚刚接触到ActionScript脚本语言的初学者,也照顾到学习过ActionScript之前版本有一定编程经验的读者。 鉴于...

    最专业的ActionScript 3.0

    ActionScript 3.0是一种强大的编程语言,主要用于实现Flex应用的业务逻辑和控制,与MXML一起构成了Flex应用程序开发的基础。ActionScript 3.0在Flash Player运行环境中执行,借助ActionScript虚拟机(ActionScript ...

    ActionScript 3.0动态绘制正弦曲线(附源码)

    ActionScript 3.0是Adobe Flash Professional和Flex SDK中用于创建富互联网应用程序(RIA)的主要编程语言。在“ActionScript 3.0动态绘制正弦曲线”这个项目中,开发者利用了AS3的强大功能来创建可视化图形,特别是...

Global site tag (gtag.js) - Google Analytics