我正在寫一個動畫循環,所以試圖優化速度。這涉及到犧牲很多可讀性,但是我想知道它是否會在某種程度上變得適得其反。例如,我可能會開始像這樣的東西:Javascript:複雜的值引用是否會影響執行速度?
var Animation = Animations[Current_Animation];
var Sequence = Animation.Sequences[Current_Sequence];
var Action = Sequence.Actions[Current_Action];
if (Action.Repeat_Count != Action.Repeat_End) Sequence.Index--;
但是,當我刪除了優化的外部變量,它出來與真正大規模的引用:
if (Animations[Current_Animation].Sequences[Current_Sequence].Actions
[Animations[Current_Animation].Sequences[Current_Sequence].Index].Repeat_Count
!= Animations[Current_Animation].Sequences[Current_Sequence].Actions
[Animations[Current_Animation].Sequences[Current_Sequence].Index].Repeat_End)
Animations[Current_Animation].Sequences[Current_Sequence].Index--;
我的循環,現在滿是這樣的怪物。所以我想知道如果解釋器不得不停止排序所有這些可能實際上比使用佔位符慢。還是它自動地知道這一切意味着什麼,而不使用任何額外的CPU?
謝謝!
現代瀏覽器在這方面可能有所改進,但避免不必要的財產查詢仍然是一件好事。此外,重複'動畫[Current_Animation] .Sequences [Current_Sequence]'一遍又一遍的感覺是錯誤的;) – 2012-03-15 15:07:05