実行時間をミリ秒単位で取得する必要があります。
この質問は2008年に行ったものです。当時の一般的な回答は new Date().getTime()]1を使うのが一般的でしたが、現在では 標準的なperformance.now()APIを使用する方が 適切です。そのため、受け入れられた回答をこの回答に変更します。
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
:`performance
をインポート]2する必要があります。
console.time('someFunction');
someFunction(); // Whatever is timed goes between the two "console.time"
console.timeEnd('someFunction');
注意: time()
およびtimeEnd()
メソッドに渡される文字列は、
(タイマーが期待通りに終了するために)一致しなければなりません。
console.time()
のドキュメントです。
use new Date().getTime().
getTime()メソッドは、1970年1月1日の午前0時からのミリ秒数を返します。
ex.
var start = new Date().getTime();
for (i = 0; i < 50000; ++i) {
// do something
}
var end = new Date().getTime();
var time = end - start;
alert('Execution time: ' + time);