本文翻译自:How do you performance test JavaScript code?
CPU Cycles, Memory Usage, Execution Time, etc.? CPU周期,内存使用率,执行时间等?
Added: Is there a quantitative way of testing performance in JavaScript besides just perception of how fast the code runs? 补充:除了感知代码运行的速度之外,还有定量的测试JavaScript性能的方法吗?
#1楼
参考:https://stackoom.com/question/SyG/您如何性能测试JavaScript代码
#2楼
Here is a simple function that displays the execution time of a passed in function: 这是一个简单的函数,显示传入的函数的执行时间:
var perf = function(testName, fn) {
var startTime = new Date().getTime();
fn();
var endTime = new Date().getTime();
console.log(testName + ": " + (endTime - startTime) + "ms");
}
#3楼
The golden rule is to NOT under ANY circumstances lock your users browser. 黄金法则是在任何情况下均不得锁定用户浏览器。 After that, I usually look at execution time, followed by memory usage (unless you're doing something crazy, in which case it could be a higher priority). 在那之后,我通常会先看执行时间,然后看内存使用情况(除非您做了疯狂的事情,在这种情况下,它可能是更高的优先级)。
#4楼
我发现执行时间是最好的方法。
#5楼
You could use this: http://getfirebug.com/js.html . 您可以使用: http : //getfirebug.com/js.html 。 It has a profiler for JavaScript. 它具有JavaScript的探查器。
#6楼
Some people are suggesting specific plug-ins and/or browsers. 有人建议使用特定的插件和/或浏览器。 I would not because they're only really useful for that one platform; 我不会,因为他们只为一个平台, 真正有用的; a test run on Firefox will not translate accurately to IE7. 在Firefox上运行的测试无法正确转换为IE7。 Considering 99.999999% of sites have more than one browser visit them, you need to check performance on all the popular platforms. 考虑到99.999999%的网站有多个浏览器访问它们,因此您需要检查所有流行平台的性能。
My suggestion would be to keep this in the JS. 我的建议是将此保留在JS中。 Create a benchmarking page with all your JS test on and time the execution. 创建一个基准测试页面,其中包含所有JS测试并计时执行时间。 You could even have it AJAX-post the results back to you to keep it fully automated. 您甚至可以使用AJAX将结果发布回给您,以使其完全自动化。
Then just rinse and repeat over different platforms. 然后,只需冲洗并在不同的平台上重复即可。