我需要获取以毫秒为单位的执行时间.
我最初是在2008年问这个问题的.当时被接受的答案是使用
new Date().getTime()
.然而,我们现在都同意使用标准performance.now()
API更合适.因此,我将接受的答案改为这个答案.
我需要获取以毫秒为单位的执行时间.
我最初是在2008年问这个问题的.当时被接受的答案是使用
new Date().getTime()
.然而,我们现在都同意使用标准performance.now()
API更合适.因此,我将接受的答案改为这个答案.
var startTime = performance.now()
doSomething() // <---- measured code goes between startTime and endTime
var endTime = performance.now()
console.log(`Call to doSomething took ${endTime - startTime} milliseconds`)
在
Node.js
中,需要导入performance
类
导入性能
const { performance } = require('perf_hooks');
console.time('doSomething')
doSomething() // <---- The function you're measuring time for
console.timeEnd('doSomething')
102
传递给time()
和timeEnd()
方法的字符串必须匹配
(for the timer to finish as expected).
console.time()
documentations: