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?
Myrne Stol Aug 12 '13 at 22:05 2013-08-12 22:05
source share