Carlos Melo
Carlos Melo

Reputation: 3152

Is there any way to get current time in nanoseconds using JavaScript?

So, I know I can get current time in milliseconds using JavaScript. But, is it possible to get the current time in nanoseconds instead?

Upvotes: 89

Views: 96126

Answers (9)

Jeffrey Yasskin
Jeffrey Yasskin

Reputation: 5682

Achieve microsecond accuracy in most browsers using:

window.performance.now() * 1000

See also:

Upvotes: 97

Inertial Ignorance
Inertial Ignorance

Reputation: 563

As Jeffrey's answer said, window.performance.now() gives microsecond accuracy on most browsers. However, the actual value it returns is in milliseconds; it's just that its decimal portion enables microseconds to be calculated. I.e.: window.performance.now() * 1000.

Upvotes: 0

Darren Steven
Darren Steven

Reputation: 67

I was looking for something similar to this, this is what i came up with in the end, it may not be 100% accurate but its the closest i could get.

var precision = 3;

var timestring = new Date().toLocaleTimeString() + "." + (((performance.now() + performance.timeOrigin)) / 1000).toFixed(precision).toString().split(".")[1]);

for (i=0;i<1000;i++){
  var precision = 3;
  var timestring = new Date().toLocaleTimeString() + "." + (((performance.now() + performance.timeOrigin)) / 1000).toFixed(precision).toString().split(".")[1];
  var newEl = document.createElement('div');
  
  newEl.innerHTML = timestring;
  document.getElementById("timestamps").appendChild(newEl);
}
<p>Timstamps:</p>
<div id="timestamps"></div>

Upvotes: -1

Milliseconds since the UNIX epoch, with the microseconds resolution.

performance.timing.navigationStart has been deprecated! Use the following instead:

(performance.now() + performance.timeOrigin)

Relevant quotes from the specification

This specification defines an API that provides the time origin, and current time in sub-millisecond resolution, such that it is not subject to system clock skew or adjustments.

The timeOrigin attribute MUST return a DOMHighResTimeStamp representing the high resolution time of the time origin timestamp for the relevant global object of the Performance object.

The time origin timestamp is the high resolution time value at which time origin is zero.

The time origin is the time value from which time is measured

The now() method MUST return the current high resolution time.

The current high resolution time is the high resolution time from the time origin to the present time (typically called “now”).


Note that actually it is not that accurate for security reasons (to prevent side-channel attacks)

This specification defines an API that provides sub-millisecond time resolution, which is more accurate than the previously available millisecond resolution exposed by DOMTimeStamp. However, even without this new API an attacker may be able to obtain high-resolution estimates through repeat execution and statistical analysis. To ensure that the new API does not significantly improve the accuracy or speed of such attacks, the minimum resolution of the DOMHighResTimeStamp type should be inaccurate enough to prevent attacks: the current minimum recommended resolution is no less than 5 microseconds and, where necessary, should be set higher by the User Agent to address privacy and security concerns due to architecture or software constraints, or other considerations.

Upvotes: 14

1111161171159459134
1111161171159459134

Reputation: 1215

Yes! Try the excellent sazze's nano-time

let now = require('nano-time');
now(); // '1476742925219947761' (returns as string due to JS limitation)

Upvotes: 6

Tahsin Turkoz
Tahsin Turkoz

Reputation: 5142

In Server side environments like Node.js you can use the following function to get time in nanosecond

function getNanoSecTime() {
  var hrTime = process.hrtime();
  return hrTime[0] * 1000000000 + hrTime[1];
}

Also get micro seconds in a similar way as well:

function getMicSecTime() {
  var hrTime = process.hrtime();
  return hrTime[0] * 1000000 + parseInt(hrTime[1] / 1000);
}

Upvotes: 10

Ronenz
Ronenz

Reputation: 2126

Building on Jeffery's answer, to get an absolute time-stamp (as the OP wanted) the code would be:

var TS = window.performance.timing.navigationStart + window.performance.now();

result is in millisecond units but is a floating-point value reportedly "accurate to one thousandth of a millisecond".

Upvotes: 12

Ryan Lynch
Ryan Lynch

Reputation: 7776

JavaScript records time in milliseconds, so you won't be able to get time to that precision. The smart-aleck answer is to "multiply by 1,000,000".

Upvotes: 2

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385088

No. There is not a chance you will get nanosecond accuracy at the JavaScript layer.

If you're trying to benchmark some very quick operation, put it in a loop that runs it a few thousand times.

Upvotes: 5

Related Questions