編寫(xiě)高效率的AS3代碼的小技巧
Array & Object constructing
構(gòu)造數(shù)組和對(duì)象的時(shí)候,new Array() and new Object()要比 [] and {}慢3倍的時(shí)間
Index Number type for Arrays
數(shù)組的數(shù)字索引類(lèi)型
ist[int(0)] 比list[0]要快
Create Array vs. Updating Array
再循環(huán)語(yǔ)句中避免多次創(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的方式來(lái)代替使用數(shù)組函數(shù)push
比如
list[list.length] = data; 要比直接用push快600%;
Array emptying - length 0 vs. A new Array
如果你需要設(shè)置一個(gè)空數(shù)組,有一個(gè)方便的辦法去選擇,就是通過(guò)設(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)的類(lèi)型的變量進(jìn)行比較 同類(lèi)型的比較效率高的多
Type casting comparison 強(qiáng)制轉(zhuǎn)換類(lèi)型對(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的封裝類(lèi)2009-02-02as3 rollOver or mouseOver使用說(shuō)明
rollOver與mouseOver同樣在鼠標(biāo)移到目標(biāo)上時(shí)觸發(fā)事件,細(xì)微區(qū)別在于,mouseOver的bubbles等于true,而rollOver的bubbles是false.2009-10-10Google Analytics在Flash cs3下的使用教程分析
因?yàn)楣ぷ鞯脑颍罱褂玫紾oogle Analytics組件,這個(gè)組件在網(wǎng)上的資料很多,但是大部分都是詳談組件的優(yōu)勢(shì)的,具體的使用沒(méi)有很詳細(xì)的說(shuō)明2009-02-02AS3自寫(xiě)類(lèi)整理筆記 ClassLoader類(lèi)
在用flash做項(xiàng)目的時(shí)候,把一些元件,通過(guò)設(shè)置鏈接類(lèi),然后使用這個(gè)類(lèi),通過(guò)getClass方法即可把這個(gè)素材拿下來(lái)2008-06-06AS3 navigateToURL導(dǎo)致ExternalInterface 執(zhí)行失敗問(wèn)題
AS3 navigateToURL導(dǎo)致ExternalInterface 執(zhí)行失敗問(wèn)題2009-02-02AS3 中的package(包)應(yīng)用實(shí)例代碼
初學(xué)者在學(xué)習(xí)AS3時(shí)會(huì)遇到什么樣的問(wèn)題呢?只有從初學(xué)的角度來(lái)實(shí)踐,才能知道,package 這個(gè)高手們必玩的內(nèi)容,對(duì)初學(xué)者來(lái)說(shuō)或許就有一些困惑。2008-08-08AS3自寫(xiě)類(lèi)整理筆記:ByteLoader類(lèi)
該類(lèi)的主要功能是把swf,jpg,png,gif等文件以字節(jié)的形式加載進(jìn)來(lái) 以便于使用Loader.loadBytes方法,重復(fù)加載使用素材 如果圖片格式為jpg,并且是漸進(jìn)式格式j(luò)peg,那么該類(lèi)還可以幫助你邊加載邊顯示2008-06-06