How to get microtime in Node.js?

How can I get the exact timestamp in Node.js?

ps My version of Node.js is 0.8.X, and node -microtime extension does not work for me (installation failure)

+79
javascript v8
Jul 30 '12 at 16:32
source share
11 answers

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

Update: As vaughan pointed out, process.hrtime() available within Node.js - its resolution is nanoseconds, and therefore it is much higher, also this does not mean that it should be more accurate.

PS: To be more clear, process.hrtime() returns you an Array tuple containing the current real time with high resolution in seconds [seconds, nanoseconds]

+81
Jul 30 2018-12-12T00:
source share

In Node.js, "high resolution time" is available through process.hrtime . It returns an array with the first element in seconds, and the second - the remaining nanoseconds.

To get the current time in microseconds, do the following:

 var hrTime = process.hrtime() console.log(hrTime[0] * 1000000 + hrTime[1] / 1000) 

(Thanks to itaifrenkel for indicating errors when converting above.)

In modern browsers, microsecond precision time is available as performance.now . See https://developer.mozilla.org/en-US/docs/Web/API/Performance/now for documentation.

I have implemented this function for Node.js based on process.hrtime , which is relatively difficult to use if you only want to calculate the time difference between two points in the program. See http://npmjs.org/package/performance-now . Behind the specification, this function reports time in milliseconds, but it is a float accurate to milliseconds.

In version 2.0 of this module, these milliseconds refer to the beginning of the node process ( Date.now() - (process.uptime() * 1000) ). You need to add this to the result if you want the timestamp to be similar to Date.now() . Also note that you must double-check Date.now() - (process.uptime() * 1000) . Both Date.now and process.uptime very unreliable for accurate measurements.

To get the current time in microseconds, you can use something like this.

 var loadTimeInMS = Date.now() var performanceNow = require("performance-now") console.log((loadTimeInMS + performanceNow()) * 1000) 

See also: Does JavaScript provide a high-resolution timer?

+161
Aug 12 '13 at 22:05
source share
 now('milli'); // 120335360.999686 now('micro') ; // 120335360966.583 now('nano') ; // 120335360904333 

It is known that now is:

 const now = (unit) => { const hrTime = process.hrtime(); switch (unit) { case 'milli': return hrTime[0] * 1000 + hrTime[1] / 1000000; case 'micro': return hrTime[0] * 1000000 + hrTime[1] / 1000; case 'nano': return hrTime[0] * 1000000000 + hrTime[1]; default: return now('nano'); } }; 
+14
Aug 12 '16 at 13:38 on
source share

There is also https://github.com/wadey/node-microtime :

 > var microtime = require('microtime') > microtime.now() 1297448895297028 
+6
Sep 29 '15 at 16:40
source share

Node.js nanotimer

I wrote a library / wrapper object for node.js on top of the process.hrtime function call. It has useful features such as synchronizing synchronous and asynchronous tasks defined in seconds, milliseconds, micro and even nano and following the syntax of the built-in javascript timer to be familiar.

Timer objects are also discrete, so you can have as many as you want, each with its own setTimeout or setInterval process.

It is called nanotimer . Check this!

+5
Aug 12 '13 at 19:23
source share

BigInt data BigInt supported since Node.js 10.7.0. (see also blog post ). For these supported versions of Node.js, the process.hrtime([time]) method is now considered obsolete and is replaced by the process.hrtime.bigint() method.

bigint version of the process.hrtime() method that returns the current high resolution in real time in the form of bigint .

 const start = process.hrtime.bigint(); // 191051479007711n setTimeout(() => { const end = process.hrtime.bigint(); // 191052633396993n console.log('Benchmark took ${end - start} nanoseconds'); // Benchmark took 1154389282 nanoseconds }, 1000); 



TL; dr

  • Node.js 10.7. 0+ - use process.hrtime.bigint()
  • Otherwise - use process.hrtime()
+4
Feb 03 '19 at 22:19
source share

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

 function getTimeMSFloat() { var hrtime = process.hrtime(); return ( hrtime[0] * 1000000 + hrtime[1] / 1000 ) / 1000; } 
+2
Oct 30 '15 at 11:38
source share

There are npm packages that bind to the gettimeofday () function of the system, which returns a microsecond precision label in Linux. Search for npm gettimeofday . Calling C is faster than process.hrtime()

0
Sep 10 '15 at 23:22
source share

Rewrite to help quickly understand:

 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
Nov 28 '17 at 4:56 on
source share

Get hrtime as a single number on 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 one argument is passed to it, will use the first element of the array as the initial value of accumulator . You can use 0 as the initial value, and this will also work, but why do the extra * 0 .

0
Jul 11 '19 at 21:00
source share

better?

 Number(process.hrtime().join('')) 
-5
Feb 27 '18 at 16:34
source share



All Articles