[討論] function
最近試著在調整 mobile 網頁的效能,嘗試以下三種 function 建立法:
function testOne(...) {
...
};
var testTwo = function(...) {
...
};
var testThree = new function(...) {
...
};
讓我好奇的是... 參考這兩個網頁:
* http://jsperf.com/run-time-vs-parse-time-functions
* http://jsperf.com/function-declaration-vs-function-expression
testOne 與 testTwo 有相反的效能。
依照測試結果,我的理解如下:
* testOne
- typeof 是 function,屬 function declaration
- 在 Parse time 處理,可於程式碼區段前呼叫
- 呼叫方式為 testOne(...);
- 不呼叫就不會執行
* testTwo
- typeof 是 function,屬 function expression
- 在 Run time 處理,處理前無法呼叫
- 呼叫方式為 testTwo(...);
- 不呼叫就不會執行
* testThree
- typeof 是 object,屬 object declaration
- 在 Run time 處理,處理前無法呼叫
- 呼叫方式為 testThree.constructor(...);
- Run time 期間會被執行一次,此次執行不會傳入參數
而根據 http://moduscreate.com/javascript-performance-tips-tricks/ 看來,
testThree 有絕佳的的效能。
請問板上有前輩曾對這三種程式寫法進行 benchmarking 嗎?
懇請不吝賜教啊... <(_ _)>
PS. 剛做了個測試 http://jsperf.com/javascript-function ...
--
我不寂寞, 因為煙在我口,
我不寂寞, 因為酒在我手,
我不寂寞, 因為還有寂寞陪著我.
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.132.97.142
※ 文章網址: http://www.ptt.cc/bbs/Ajax/M.1403803743.A.B81.html
※ 編輯: JoeHorn (220.132.97.142), 06/27/2014 02:08:28
推
06/27 04:50, , 1F
06/27 04:50, 1F
→
06/27 04:51, , 2F
06/27 04:51, 2F
不好意思,我寫反了,已修正,謝謝!
※ 編輯: JoeHorn (220.132.97.142), 06/27/2014 11:02:13