構(gòu)造數(shù)組和對(duì)象的時(shí)候,new Array() and new Object()要比 [] and {}慢3倍的時(shí)間
數(shù)組的數(shù)字索引類型
ist[int(0)] 比list[0]要快
再循環(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.
delete一個(gè)對(duì)象的屬性要比把該屬性設(shè)置為null 更昂貴,所以對(duì)于對(duì)象的屬性最好設(shè)置為null
多次嵌套循環(huán)效率差,所以最好保證循環(huán)在2層以內(nèi)
如果在時(shí)間幀上的函數(shù)很長(zhǎng)而且執(zhí)行時(shí)間長(zhǎng),最好,把該函數(shù)分成多個(gè)小的函數(shù)執(zhí)行。
這樣可以縮短執(zhí)行時(shí)間提高效率
盡量最小化函數(shù)的參數(shù)個(gè)數(shù)
Scoping function.apply is a little bit slower than not so if you don't have to then don't.
用設(shè)置index的方式來代替使用數(shù)組函數(shù)push
比如
list[list.length] = data; 要比直接用push快600%;
如果你需要設(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ì)更好
將變量聲明在一行中,要比聲明多行更好,效率更高
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;
乘法的運(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.
盡量用短的變量名
|
新聞熱點(diǎn)
疑難解答