Flex常用代碼

來源:互聯網
上載者:User

看來大家對Array情有獨鐘,對它討論的很多,這兩天看了篇關於flex如何減少代碼量減少記憶體消耗的文章,其實也像是代碼重整,翻譯整理了一下。
前面主要是關於Array的,後面涉及很多,不過有些估計不適用於Flex4了。
1. Avoid the new operator when creating Arrays
盡量避免用new操作符來建立數組
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
沒有子類的類應該設定為final
public final class StringUtils

9. Length of method/variable names doesn’t matter in ActionScript 3.0 (true in other langs)
在ActionScript 3.0中方法名/變數名的長度不影響程式運行
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語句和switch語句佔用的記憶體並無差別
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語句,其中應該放經常使用的運算式
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.)
AVM 在迴圈計算裡將int提升到Number類型了(這樣計算會更快)

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;
避免使用uint,它有些慢

16. Use integers for iterations
迴圈中使用int而不是Number
(var i: int = 0; i < n; i++) NOT for (var i: Number = 0; i < n; i++)

17. Don’t use int with decimals
int類型不接受小數
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
NOT:
for (var i=0;i< myArray.lengh;i++){ }

21. Use RegEx for validation, use string methods for searching
使用Regex來進行驗證字串,使用String類的方法進行查詢
// 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:
遵循Flex組件的建立模式:
createChildren(); commitProperties(); updateDisplayList();

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

25. Avoid Repeaters for scrollable data
資料很大時避免使用Repeaters

26. Avoid the setStyle() method (One of the most expensive calls in the Flex framework)
避免使用setStyle()方法????

27. Using too many containers dramatically reduces the performance of your application
動態建立太多的容器會降低程式的效能

text="Label 4" />

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:
mxml頁面的起始標籤不一定是容器

29. Remove unnecessary container wrappers to reduce container nesting
移除無用的組件嵌套

30. Avoid: The VBox container inside an tag, (eliminates redundancy)
避免把Box放到另一個容器(不過,border好像需要)

31. Avoid: VBox container inside an mx:Application tag, (eliminates redundancy)
<?xml version="1.0" encoding="utf-8"?>

/>

NOT:
<?xml version="1.0" encoding="utf-8"?>

32. Set the recycleChildren property to true to improve a Repeater object’s performance (re-uses previously created children instead of creating new ones)
使用Repeater時設定recycleChildren為true(但是,組件之間的狀態會相互影響)

dataProvider="{repeaterData}">

33. Keep framerate set at 60 fps or lower
設定framerate(幀頻率)小於60
<?xml version="1.0" encoding="utf-8"?>

34. Avoid multiple display manipulations per frame
避免每一幀中有太多的現實動作

35. Code against ENTER_FRAME events instead of Timer events
使用ENTER_FRAME 事件代替 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:
使用多頁幀時延遲物件的建立

37. Alpha = 0 is not the same as visible = false (Objects marked invisible are passed over)
Alpha = 0 不同於 visible = false
loginButton.visible = false;
NOT:
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.