Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I need to get execution time in milliseconds. (我需要获取执行时间(以毫秒为单位)。)

I originally asked this question back in 2008. The accepted answer then was to use new Date().getTime() However, we can all agree now that using the standard performance.now() API is more appropriate. (我最初是在2008年问这个问题的。当时接受的答案是使用new Date()。getTime()。但是,我们现在都同意使用标准performance.now() API更合适。) I am therefore changing the accepted answer to this one. (因此,我正在改变对此的公认答案。)

  ask by Julius A translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
361 views
Welcome To Ask or Share your Answers For Others

1 Answer

Using performance.now() : (使用performance.now() :)

var t0 = performance.now();

doSomething();   // <---- The function you're measuring time for 

var t1 = performance.now();
console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.");

NodeJs : it is required to import the performance class (NodeJs :需要导入performance类)


Using console.time : (使用console.time :) (non-standard) ((非标准)) ( living standard ) (( 生活水平 ))

console.time('someFunction');

someFunction(); // Whatever is timed goes between the two "console.time"

console.timeEnd('someFunction');

Note : (注意事项 )
The string being pass to the time() and timeEnd() methods must match (传递给time()timeEnd()方法的字符串必须匹配)
(for the timer to finish as expected). ((以使计时器按预期完成)。)

console.time() documentations: (console.time()文档:)

  1. NodeJS documentation regarding (关于NodeJS的文档)
  2. MDN (client-side) documentation (MDN(客户端)文档)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...