[討論] function

看板Ajax作者 (每天都在公司玩OLG)時間10年前 (2014/06/27 01:29), 10年前編輯推噓1(101)
留言2則, 1人參與, 最新討論串1/1
最近試著在調整 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
我測試的三個結果都是type1快耶。
06/27 04:50, 1F

06/27 04:51, , 2F
還有你給的連結type1是寫parse time耶。是搞反了嗎 ?
06/27 04:51, 2F
不好意思,我寫反了,已修正,謝謝! ※ 編輯: JoeHorn (220.132.97.142), 06/27/2014 11:02:13
文章代碼(AID): #1Jh5XVk1 (Ajax)