How to get a microtime in Node.js?

后端 未结 13 1186
情深已故
情深已故 2020-12-04 13:41

How can I get the most accurate time stamp in Node.js?

ps My version of Node.js is 0.8.X and the node-microtime extension doesn\'t work for me (crash on install)

13条回答
  •  再見小時候
    2020-12-04 14:32

    Get hrtime as single number in one line:

    const begin = process.hrtime();
    // ... Do the thing you want to measure
    const nanoSeconds = process.hrtime(begin).reduce((sec, nano) => sec * 1e9 + nano)
    

    Array.reduce, when given a single argument, will use the array's first element as the initial accumulator value. One could use 0 as the initial value and this would work as well, but why do the extra * 0.

提交回复
热议问题