構造數組和對象的時候,new Array() and new Object()要比 [] and {}慢3倍的時間
數組的數字索引類型
ist[int(0)] 比list[0]要快
再循環語句中避免多次創建數組,最好創建一次用多次更新內容替換
Nulling Array vs. Splicing Array
對于龐大的數組而言就行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一個對象的屬性要比把該屬性設置為null 更昂貴,所以對于對象的屬性最好設置為null
多次嵌套循環效率差,所以最好保證循環在2層以內
如果在時間幀上的函數很長而且執行時間長,最好,把該函數分成多個小的函數執行。
這樣可以縮短執行時間提高效率
盡量最小化函數的參數個數
Scoping function.apply is a little bit slower than not so if you don't have to then don't.
用設置index的方式來代替使用數組函數push
比如
list[list.length] = data; 要比直接用push快600%;
如果你需要設置一個空數組,有一個方便的辦法去選擇,就是通過設置它的length屬性為0
或者你會認為這么做是不錯的選擇,原因是它能節省內存,但是事實上這樣做的執行速度不如直接new array的效率高
當然,如果你需要在一次循環中清除多于510個數組為空時,用length設置為0的時候會更好
將變量聲明在一行中,要比聲明多行更好,效率更高
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;
如果你想去交換變量,但是又不想創建新的變量的時候,可以用xor 如:Using Xor to swap variables
a = a^b;
b = a^b;
a = a^b;
乘法的運算速率總是比出發快,比如5000/1000 要比 5000*0.001快130%;
When type casting the keyword 建議使用對應的類型的變量進行比較 同類型的比較效率高的多 Type casting comparison 強制轉換類型對比
as
is 250% more efficient than casting by Type(item);
Though surprisingly not using either is about 1400% more efficient.
盡量用短的變量名
新聞熱點
疑難解答