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

用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

转载于:https://www.cnblogs.com/yfish/p/7081073.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值