Javascript 细粒度性能调整
我一直在编写我的第一个 jQuery 插件,并努力寻找一种方法来计算不同代码段的运行时间。
我可以使用 firebug 和 console.time/profile。然而,似乎因为我的代码执行得如此之快,所以我没有得到配置文件的结果,并且随着时间的推移,它输出了 0 毫秒。 (Firebug 分析问题:“没有要分析的活动”)
有没有办法在 JavaScript 中获得比毫秒更详细的时间?
更新:我已将要测试的代码放入循环一百万次的循环中,但这不是理想的解决方案。
I have been writing my first jQuery plugin and struggling to find a means to time how long different pieces of code take to run.
I can use firebug and console.time/profile. However, it seems that because my code executes so fast I get no results with profile and with time it spits out 0ms. (Firebug profiling issue: "no activity to profile")
Is there a way to get the time at a greater level of detail that milliseconds in javascript?
UPDATE: I've put code that I want to test in a loop that loops it a million times, but it's not an ideal solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要多次运行测试,然后计算每秒执行的操作数。
创建此类测试用例的最简单方法是使用jsPerf 网站。它将生成如下测试用例: http://jsperf.com/prime-numbers
You need to run your tests a number of times and then calculate how many operations per second are executed.
The easiest way to create such a test case is to use the jsPerf website. It will generate a test case like this: http://jsperf.com/prime-numbers
听起来有点奇怪,但是:买一台非常旧的电脑。 Pentium 2 就可以了。这不是一个昂贵的解决方案,您可以将整台机器奉献给您的完美主义。很高兴看到脚本运行并构建页面元素的速度足够慢,足以看到它。
It sounds a bit weird but: Buy a really old computer. A Pentium 2 would do nicely. It is not an expensive solution and you get to dedicate a whole machine to your perfectionism. It's lovely seeing the scripts run and build up the page elements slow enough to see it.