102

質問は簡単だと思います。

nodejs V8 エンジンの window.performance.now() に似たものを探しています。

今、私はちょうど使用しています:-

var now = Date.now();
//do some processing..
console.log("time elapsed:", Date.now() - now);

しかし、ここで定義されているため、 window.performance.now() は日付を使用するよりもはるかに正確であると読みました。

4

8 に答える 8

30

process.hrtime()マイクロ秒ではなくミリ秒を返すショートカットは次のとおりです。

function clock(start) {
    if ( !start ) return process.hrtime();
    var end = process.hrtime(start);
    return Math.round((end[0]*1000) + (end[1]/1000000));
}

使用法:

var start = clock();
// do some processing that takes time
var duration = clock(start);
console.log("Took "+duration+"ms");

「Took 200ms」のようなものを出力します

于 2016-01-24T00:02:02.497 に答える
17

どうですか?

console.time('FooTimer');
// do the work
console.timeEnd('FooTimer');
于 2015-07-27T13:51:44.570 に答える