Skip to content

Commit 5ba811e

Browse files
authored
fix: Check that performance is available before calling it in RN (#2924)
1 parent f84b963 commit 5ba811e

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

packages/utils/src/misc.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,19 @@ const performanceFallback: CrossPlatformPerformance = {
271271
* Performance wrapper for react native as performance.now() has been found to start off with an unusual offset.
272272
*/
273273
function getReactNativePerformanceWrapper(): CrossPlatformPerformance {
274-
const INITIAL_OFFSET = performance.now();
275-
276-
return {
277-
now(): number {
278-
return performance.now() - INITIAL_OFFSET;
279-
},
280-
timeOrigin: INITIAL_TIME,
281-
};
274+
// Performance only available >= RN 0.63
275+
const { performance } = getGlobalObject<Window>();
276+
if (performance && typeof performance.now === 'function') {
277+
const INITIAL_OFFSET = performance.now();
278+
279+
return {
280+
now(): number {
281+
return performance.now() - INITIAL_OFFSET;
282+
},
283+
timeOrigin: INITIAL_TIME,
284+
};
285+
}
286+
return performanceFallback;
282287
}
283288

284289
export const crossPlatformPerformance: CrossPlatformPerformance = ((): CrossPlatformPerformance => {

0 commit comments

Comments
 (0)