作者JoeHorn (每天都在公司玩OLG)
看板Ajax
标题[讨论] function
时间Fri Jun 27 01:28:48 2014
最近试着在调整 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://webptt.com/cn.aspx?n=bbs/Ajax/M.1403803743.A.B81.html
※ 编辑: JoeHorn (220.132.97.142), 06/27/2014 02:08:28
1F:推 up9cloud:我测试的三个结果都是type1快耶。 06/27 04:50
2F:→ up9cloud:还有你给的连结type1是写parse time耶。是搞反了吗 ? 06/27 04:51
不好意思,我写反了,已修正,谢谢!
※ 编辑: JoeHorn (220.132.97.142), 06/27/2014 11:02:13