@@ -78,12 +78,14 @@ let cachedTimeOrigin: number | null | undefined = null;
7878
7979/**
8080 * Gets the time origin and the mode used to determine it.
81+ *
82+ * Unfortunately browsers may report an inaccurate time origin data, through either performance.timeOrigin or
83+ * performance.timing.navigationStart, which results in poor results in performance data. We only treat time origin
84+ * data as reliable if they are within a reasonable threshold of the current time.
85+ *
8186 * TODO: move to `@sentry/browser-utils` package.
8287 */
8388function getBrowserTimeOrigin ( ) : number | undefined {
84- // Unfortunately browsers may report an inaccurate time origin data, through either performance.timeOrigin or
85- // performance.timing.navigationStart, which results in poor results in performance data. We only treat time origin
86- // data as reliable if they are within a reasonable threshold of the current time.
8789 const { performance } = GLOBAL_OBJ as typeof GLOBAL_OBJ & Window ;
8890 if ( ! performance ?. now ) {
8991 return undefined ;
@@ -93,11 +95,13 @@ function getBrowserTimeOrigin(): number | undefined {
9395 const performanceNow = performance . now ( ) ;
9496 const dateNow = Date . now ( ) ;
9597
96- // if timeOrigin isn't available set delta to threshold so it isn't used
97- const timeOriginDelta = performance . timeOrigin
98- ? Math . abs ( performance . timeOrigin + performanceNow - dateNow )
99- : threshold ;
100- const timeOriginIsReliable = timeOriginDelta < threshold ;
98+ const timeOrigin = performance . timeOrigin ;
99+ if ( typeof timeOrigin === 'number' ) {
100+ const timeOriginDelta = Math . abs ( timeOrigin + performanceNow - dateNow ) ;
101+ if ( timeOriginDelta < threshold ) {
102+ return timeOrigin ;
103+ }
104+ }
101105
102106 // TODO: Remove all code related to `performance.timing.navigationStart` once we drop support for Safari 14.
103107 // `performance.timeSince` is available in Safari 15.
@@ -110,23 +114,16 @@ function getBrowserTimeOrigin(): number | undefined {
110114 // Date API.
111115 // eslint-disable-next-line deprecation/deprecation
112116 const navigationStart = performance . timing ?. navigationStart ;
113- const hasNavigationStart = typeof navigationStart === 'number' ;
114- // if navigationStart isn't available set delta to threshold so it isn't used
115- const navigationStartDelta = hasNavigationStart ? Math . abs ( navigationStart + performanceNow - dateNow ) : threshold ;
116- const navigationStartIsReliable = navigationStartDelta < threshold ;
117-
118- // TODO: Since timeOrigin explicitly replaces navigationStart, we should probably remove the navigationStartIsReliable check.
119- if ( timeOriginIsReliable && timeOriginDelta <= navigationStartDelta ) {
120- return performance . timeOrigin ;
121- }
122-
123- if ( navigationStartIsReliable ) {
124- return navigationStart ;
117+ if ( typeof navigationStart === 'number' ) {
118+ const navigationStartDelta = Math . abs ( navigationStart + performanceNow - dateNow ) ;
119+ if ( navigationStartDelta < threshold ) {
120+ return navigationStart ;
121+ }
125122 }
126123
127- // TODO: We should probably fall back to Date.now() - performance.now(), since this is still more accurate than just Date.now() (?)
128- // Either both timeOrigin and navigationStart are skewed or neither is available, fallback to Date.
129- return dateNow ;
124+ // Either both timeOrigin and navigationStart are skewed or neither is available, fallback to subtracting
125+ // `performance.now()` from ` Date.now()` .
126+ return dateNow - performanceNow ;
130127}
131128
132129/**
0 commit comments