Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34.3k
Description
- Version: v9.x / master
- Platform: all
- Subsystem: perf_hooks
$ ./node -p 'new Date(perf_hooks.performance.timeOrigin).toString()' Sat Jan 03 1970 08:35:02 GMT+0800 (CST) while in browsers
>newDate(performance.timeOrigin).toString()"Thu Dec 28 2017 15:02:33 GMT+0800 (CST)"performance.timeOrigin is spec'd to return the time origin timestamp, which is a high-resolution UNIX time, rather than a time from an arbitrary position in the past (what it currently returns).
Unfortunately, after looking over the libuv documentation I could not find a function that returns something akin to clock_gettime(CLOCK_REALTIME, tp) on POSIX systems. The ideal solution is, of course, adding such a method to libuv. But if it is not possible to do so in a timely manner, I did outline a hack that allows me to get the correct high-precision value of timeOrigin in jsdom/jsdom#2094 (comment), reproduced below:
const{ timeOrigin }=process.binding(...);// Currently:// performance.timeOrigin = timeOrigin;// Offset between the time returned by process.hrtime() and Date.now(). A.k.a. the |t1|// in W3C [HR-TIME] spec.consthrtimeOffset=getHrtimeOffset();consttimeOriginTimestamp=hrtimeOffset+timeOrigin;performance.timeOrigin=timeOriginTimestamp;functiongetHrtimeOffset(){// Wait at most 1 millisecond, a negligible amount of time for initialization.letcur=Date.now();constnext=cur+1;while(cur!==next){cur=Date.now();}// At this point cur "just" became equal to the next millisecond -- the unseen digits after cur// are approximately all 0, and cur is the closest to the actual value of the UNIX time.// Now, get the current global monotonic clock value:const[hrSec,hrNS]=process.hrtime();constglobalMonotonicClockMS=hrSec*1e3+hrNS/1e6;// Let |t1| be the DOMHighResTimeStamp representing the high resolution Unix time at// which the global monotonic clock is zero.// I.e., t1 + globalMonotonicClockMS = cur// t1 = cur - globalMonotonicClockMSreturncur-globalMonotonicClockMS;}but it requires at most 1ms of initialization time when perf_hooks is required.
Thoughts?