@@ -25,6 +25,12 @@ export type EpochClockOptions = {
2525 pid ?: number ;
2626 tid ?: number ;
2727} ;
28+
29+ type MaybePerformance = typeof performance & {
30+ timeOrigin ?: number ;
31+ now ?: number ;
32+ } ;
33+
2834/**
2935 * Creates epoch-based clock utility.
3036 * Epoch time has been the time since January 1, 1970 (UNIX epoch).
@@ -36,12 +42,12 @@ export function epochClock(init: EpochClockOptions = {}) {
3642 const tid = init . tid ?? threadId ;
3743
3844 const timeOriginMs = hasTimeOrigin ( )
39- ? ( performance as { timeOrigin : number | undefined } ) . timeOrigin
40- : undefined ;
45+ ? ( performance as MaybePerformance ) . timeOrigin
46+ : Date . now ( ) ;
4147
4248 const epochNowUs = ( ) : Microseconds => {
4349 if ( hasTimeOrigin ( ) ) {
44- return msToUs ( performance . timeOrigin + performance . now ( ) ) ;
50+ return msToUs ( timeOriginMs + performance . now ( ) ) ;
4551 }
4652 return msToUs ( Date . now ( ) ) ;
4753 } ;
@@ -51,10 +57,10 @@ export function epochClock(init: EpochClockOptions = {}) {
5157 const fromEpochMs = msToUs ;
5258
5359 const fromPerfMs = ( perfMs : Milliseconds ) : Microseconds => {
54- if ( timeOriginMs === undefined ) {
55- return epochNowUs ( ) - msToUs ( performance . now ( ) - perfMs ) ;
60+ if ( hasTimeOrigin ( ) ) {
61+ return msToUs ( timeOriginMs + perfMs ) ;
5662 }
57- return msToUs ( timeOriginMs + perfMs ) ;
63+ return epochNowUs ( ) - msToUs ( performance . now ( ) - perfMs ) ;
5864 } ;
5965
6066 const fromEntryStartTimeMs = fromPerfMs ;
0 commit comments