我需要以毫秒为单位获取执行时间。
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类
Node.js
performance
进口业绩
const { performance } = require('perf_hooks');
console.time('doSomething') doSomething() // <---- The function you're measuring time for console.timeEnd('doSomething')
注意: 传递给time()andtimeEnd()方法的字符串必须匹配 (定时器才能按预期完成)。
time()
timeEnd()
console.time()文件: MDN 文档 Node.js 文档
console.time()