How to get a microtime in Node.js?

后端 未结 13 1174
情深已故
情深已故 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:26

    To work with more precision than Date.now(), but with milliseconds in float precision:

    function getTimeMSFloat() {
        var hrtime = process.hrtime();
        return ( hrtime[0] * 1000000 + hrtime[1] / 1000 ) / 1000;
    }
    
    0 讨论(0)
  • 2020-12-04 14:28

    new Date().getTime()? This gives you a timestamp in milliseconds, which is the most accurate that JS will give you.

    Update: As stated by vaughan, process.hrtime() is available within Node.js - its resolution are nanoseconds and therefore its much higher, also this doesn't mean it has to be more exact.

    PS.: Just to be clearer, process.hrtime() returns you a tuple Array containing the current high-resolution real time in a [seconds, nanoseconds]

    0 讨论(0)
  • 2020-12-04 14:29

    A rewrite to help quick understanding:

    const hrtime = process.hrtime();     // [0] is seconds, [1] is nanoseconds
    
    let nanoSeconds = (hrtime[0] * 1e9) + hrtime[1];    // 1 second is 1e9 nano seconds
    console.log('nanoSeconds:  ' + nanoSeconds);
    //nanoSeconds:  97760957504895
    
    let microSeconds = parseInt(((hrtime[0] * 1e6) + (hrtime[1]) * 1e-3));
    console.log('microSeconds: ' + microSeconds);
    //microSeconds: 97760957504
    
    let milliSeconds = parseInt(((hrtime[0] * 1e3) + (hrtime[1]) * 1e-6));
    console.log('milliSeconds: ' + milliSeconds);
    //milliSeconds: 97760957
    

    Source: https://nodejs.org/api/process.html#process_process_hrtime_time

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-04 14:34

    I'm not so proud about this solution but you can have timestamp in microsecond or nanosecond in this way:

    const microsecond = () => Number(Date.now() + String(process.hrtime()[1]).slice(3,6))
    const nanosecond = () => Number(Date.now() + String(process.hrtime()[1]).slice(3))
    
    // usage
    microsecond() // return 1586878008997591
    nanosecond()  // return 1586878009000645600
    
    // Benchmark with 100 000 iterations
    // Date.now: 7.758ms
    // microsecond: 33.382ms
    // nanosecond: 31.252ms
    

    Know that:

    • This solution works exclusively with node.js,
    • This is about 3 to 10 times slower than Date.now()
    • Weirdly, it seems very accurate, hrTime seems to follow exactly js timestamp ticks.
    • You can replace Date.now() by Number(new Date()) to get timestamp in milliseconds

    Edit:

    Here a solution to have microsecond with comma, however, the number version will be rounded natively by javascript. So if you want the same format every time, you should use the String version of it.

    const microsecondWithCommaString = () => (Date.now() + '.' + String(process.hrtime()[1]).slice(3,7))
    const microsecondWithComma = () => Number(Date.now() + '.' + String(process.hrtime()[1]).slice(3,7))
    
    microsecondWithCommaString() // return "1586883629984.8997"
    microsecondWithComma() // return 1586883629985.966
    
    0 讨论(0)
  • 2020-12-04 14:36

    Node.js nanotimer

    I wrote a wrapper library/object for node.js on top of the process.hrtime function call. It has useful functions, like timing synchronous and asynchronous tasks, specified in seconds, milliseconds, micro, or even nano, and follows the syntax of the built in javascript timer so as to be familiar.

    Timer objects are also discrete, so you can have as many as you'd like, each with their own setTimeout or setInterval process running.

    It's called nanotimer. Check it out!

    0 讨论(0)
提交回复
热议问题