編寫高效率的AS3代碼的小技巧
Array & Object constructing
構(gòu)造數(shù)組和對象的時候,new Array() and new Object()要比 [] and {}慢3倍的時間
Index Number type for Arrays
數(shù)組的數(shù)字索引類型
ist[int(0)] 比list[0]要快
Create Array vs. Updating Array
再循環(huán)語句中避免多次創(chuàng)建數(shù)組,最好創(chuàng)建一次用多次更新內(nèi)容替換
Nulling Array vs. Splicing Array
對于龐大的數(shù)組而言就行splice操作是比較耗成本的,要盡量避免
When working with large Arrays splicing is obviously an expensive operation, you can avoid this by nulling the index and skipping it in a null scenario. If you need to splice in order to keep the Array length low. Then store these nulled indexes in another trash Array once the garbage count has reached a limit you've defined loop through the numerically sorted trash indexes deleting splices in bulk. This concept is demonstrated in Tweensy.
Nulling Object vs. Delete Object
delete一個對象的屬性要比把該屬性設(shè)置為null 更昂貴,所以對于對象的屬性最好設(shè)置為null
Nesting Loops(嵌套循環(huán))
多次嵌套循環(huán)效率差,所以最好保證循環(huán)在2層以內(nèi)
Inline code vs. function references
如果在時間幀上的函數(shù)很長而且執(zhí)行時間長,最好,把該函數(shù)分成多個小的函數(shù)執(zhí)行。
這樣可以縮短執(zhí)行時間提高效率
Arguments vs. variable referencing
盡量最小化函數(shù)的參數(shù)個數(shù)
Function apply scoping do it or not?
Scoping function.apply is a little bit slower than not so if you don't have to then don't.
Array push vs. Array index
用設(shè)置index的方式來代替使用數(shù)組函數(shù)push
比如
list[list.length] = data; 要比直接用push快600%;
Array emptying - length 0 vs. A new Array
如果你需要設(shè)置一個空數(shù)組,有一個方便的辦法去選擇,就是通過設(shè)置它的length屬性為0
或者你會認為這么做是不錯的選擇,原因是它能節(jié)省內(nèi)存,但是事實上這樣做的執(zhí)行速度不如直接new array的效率高
當然,如果你需要在一次循環(huán)中清除多于510個數(shù)組為空時,用length設(shè)置為0的時候會更好
Var declarations on multiple lines vs. Var declarations on a single line
將變量聲明在一行中,要比聲明多行更好,效率更高
i.e.var a:int=0, b:int=0, c:int=0;
vs.var a:int=0;
var b:int=0;
var c:int=0;
如果你想去交換變量,但是又不想創(chuàng)建新的變量的時候,可以用xor 如:
Using Xor to swap variables
a = a^b;
b = a^b;
a = a^b;
Multiplication vs. Division
乘法的運算速率總是比出發(fā)快,比如5000/1000 要比 5000*0.001快130%;
When type casting the keyword 建議使用對應(yīng)的類型的變量進行比較 同類型的比較效率高的多
Type casting comparison 強制轉(zhuǎn)換類型對比
as is 250% more efficient than casting by Type(item); Though surprisingly not using either is about 1400% more efficient.
Long vs Short variable names
盡量用短的變量名
相關(guān)文章
ActionScript 3.0中用XMLSocket與服務(wù)器通訊程序(源碼)
一個簡單的基于XMLSocket的封裝類2009-02-02
AS3 navigateToURL導(dǎo)致ExternalInterface 執(zhí)行失敗問題
AS3 navigateToURL導(dǎo)致ExternalInterface 執(zhí)行失敗問題2009-02-02

