在浏览器中怎么测试一个算法耗时?

用console.time()和console.timeEnd()即可

Console.time()

Starts a timer you can use to track how long an operation takes. You give each timer a unique name, and may have up to 10,000 timers running on a given page. When you call console.timeEnd() with the same name, the browser will output the time, in milliseconds, that elapsed since the timer was started.

See Timers in the console documentation for details and examples.

我们可以开启一个定时器来测量程序的耗时,你必须给定时器指定一个名字,每个web页面最多有10000个定时器可以设置。当你调用console.timeEnd()来终止定时器的时候,浏览器会以ms输出时间,这个时间就是程序的耗时。

 

举个栗子。

console.time(1);

var a=document.getElementById('testdiv');

console.timeEnd(1);

 

参考资料:

https://developer.mozilla.org/en-US/docs/Web/API/Console/time

posted @ 2017-06-26 16:15  eatwhat  阅读(352)  评论(0编辑  收藏  举报