編寫高效率的AS3代碼的小技巧
Array & Object constructing
構(gòu)造數(shù)組和對(duì)象的時(shí)候,new Array() and new Object()要比 [] and {}慢3倍的時(shí)間
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
對(duì)于龐大的數(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一個(gè)對(duì)象的屬性要比把該屬性設(shè)置為null 更昂貴,所以對(duì)于對(duì)象的屬性最好設(shè)置為null
Nesting Loops(嵌套循環(huán))
多次嵌套循環(huán)效率差,所以最好保證循環(huán)在2層以內(nèi)
Inline code vs. function references
如果在時(shí)間幀上的函數(shù)很長(zhǎng)而且執(zhí)行時(shí)間長(zhǎng),最好,把該函數(shù)分成多個(gè)小的函數(shù)執(zhí)行。
這樣可以縮短執(zhí)行時(shí)間提高效率
Arguments vs. variable referencing
盡量最小化函數(shù)的參數(shù)個(gè)數(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è)置一個(gè)空數(shù)組,有一個(gè)方便的辦法去選擇,就是通過設(shè)置它的length屬性為0
或者你會(huì)認(rèn)為這么做是不錯(cuò)的選擇,原因是它能節(jié)省內(nèi)存,但是事實(shí)上這樣做的執(zhí)行速度不如直接new array的效率高
當(dāng)然,如果你需要在一次循環(huán)中清除多于510個(gè)數(shù)組為空時(shí),用length設(shè)置為0的時(shí)候會(huì)更好
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)建新的變量的時(shí)候,可以用xor 如:
Using Xor to swap variables
a = a^b;
b = a^b;
a = a^b;
Multiplication vs. Division
乘法的運(yùn)算速率總是比出發(fā)快,比如5000/1000 要比 5000*0.001快130%;
When type casting the keyword 建議使用對(duì)應(yīng)的類型的變量進(jìn)行比較 同類型的比較效率高的多
Type casting comparison 強(qiáng)制轉(zhuǎn)換類型對(duì)比
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ù)器通訊程序(源碼)
一個(gè)簡(jiǎn)單的基于XMLSocket的封裝類2009-02-02Google Analytics在Flash cs3下的使用教程分析
因?yàn)楣ぷ鞯脑?,最近使用到Google Analytics組件,這個(gè)組件在網(wǎng)上的資料很多,但是大部分都是詳談組件的優(yōu)勢(shì)的,具體的使用沒有很詳細(xì)的說明2009-02-02AS3 navigateToURL導(dǎo)致ExternalInterface 執(zhí)行失敗問題
AS3 navigateToURL導(dǎo)致ExternalInterface 執(zhí)行失敗問題2009-02-02AS3 中的package(包)應(yīng)用實(shí)例代碼
初學(xué)者在學(xué)習(xí)AS3時(shí)會(huì)遇到什么樣的問題呢?只有從初學(xué)的角度來實(shí)踐,才能知道,package 這個(gè)高手們必玩的內(nèi)容,對(duì)初學(xué)者來說或許就有一些困惑。2008-08-08