From cdfe1ecd521382324aec43de14ac37c88f6ced70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Mon, 1 Sep 2025 09:18:19 -0700 Subject: [PATCH] Set up modern performance APIs if the native module is available (#53431) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/53431 Changelog: [internal] This renames `setUpPerformanceObserver` as `setUpPerformanceModern` and removes the need to call it manually. If the native module is defined, we define the whole new API. Reviewed By: javache Differential Revision: D80803626 fbshipit-source-id: ef41cb9aa959ee898d32724c102d7597e6bee84e --- packages/react-native/Libraries/Core/setUpPerformance.js | 8 +++----- ...pPerformanceObserver.js => setUpPerformanceModern.js} | 9 ++++++++- .../performance/__tests__/EventTimingAPI-itest.js | 3 --- .../webapis/performance/__tests__/LongTasksAPI-itest.js | 3 --- .../webapis/performance/__tests__/Performance-itest.js | 4 ---- .../performance/__tests__/PerformanceObserver-itest.js | 3 --- .../webapis/performance/__tests__/UserTiming-itest.js | 3 --- 7 files changed, 11 insertions(+), 22 deletions(-) rename packages/react-native/src/private/setup/{setUpPerformanceObserver.js => setUpPerformanceModern.js} (85%) diff --git a/packages/react-native/Libraries/Core/setUpPerformance.js b/packages/react-native/Libraries/Core/setUpPerformance.js index edb00ce81e2..7aa5945d6f5 100644 --- a/packages/react-native/Libraries/Core/setUpPerformance.js +++ b/packages/react-native/Libraries/Core/setUpPerformance.js @@ -4,19 +4,17 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow strict + * @flow strict-local * @format */ +import setUpPerformanceModern from '../../src/private/setup/setUpPerformanceModern'; import NativePerformance from '../../src/private/webapis/performance/specs/NativePerformance'; // In case if the native implementation of the Performance API is available, use it, // otherwise fall back to the legacy/default one, which only defines 'Performance.now()' if (NativePerformance) { - const Performance = - require('../../src/private/webapis/performance/Performance').default; - // $FlowExpectedError[cannot-write] - global.performance = new Performance(); + setUpPerformanceModern(); } else { if (!global.performance) { // $FlowExpectedError[cannot-write] diff --git a/packages/react-native/src/private/setup/setUpPerformanceObserver.js b/packages/react-native/src/private/setup/setUpPerformanceModern.js similarity index 85% rename from packages/react-native/src/private/setup/setUpPerformanceObserver.js rename to packages/react-native/src/private/setup/setUpPerformanceModern.js index 937e68fb37b..22a7ca13504 100644 --- a/packages/react-native/src/private/setup/setUpPerformanceObserver.js +++ b/packages/react-native/src/private/setup/setUpPerformanceModern.js @@ -12,13 +12,20 @@ import {polyfillGlobal} from '../../../Libraries/Utilities/PolyfillFunctions'; let initialized = false; -export default function setUpPerformanceObserver() { +export default function setUpPerformanceModern() { if (initialized) { return; } initialized = true; + const Performance = require('../webapis/performance/Performance').default; + + // We don't use `polyfillGlobal` to define this lazily because the + // `performance` object is always accessed. + // $FlowExpectedError[cannot-write] + global.performance = new Performance(); + polyfillGlobal( 'EventCounts', () => require('../webapis/performance/EventTiming').EventCounts_public, diff --git a/packages/react-native/src/private/webapis/performance/__tests__/EventTimingAPI-itest.js b/packages/react-native/src/private/webapis/performance/__tests__/EventTimingAPI-itest.js index e037b0ab6f2..8a5d7bbade8 100644 --- a/packages/react-native/src/private/webapis/performance/__tests__/EventTimingAPI-itest.js +++ b/packages/react-native/src/private/webapis/performance/__tests__/EventTimingAPI-itest.js @@ -15,12 +15,9 @@ import * as Fantom from '@react-native/fantom'; import nullthrows from 'nullthrows'; import {useState} from 'react'; import {Text, View} from 'react-native'; -import setUpPerformanceObserver from 'react-native/src/private/setup/setUpPerformanceObserver'; const NativePerformance = nullthrows(MaybeNativePerformance); -setUpPerformanceObserver(); - function sleep(ms: number) { const end = performance.now() + ms; while (performance.now() < end) {} diff --git a/packages/react-native/src/private/webapis/performance/__tests__/LongTasksAPI-itest.js b/packages/react-native/src/private/webapis/performance/__tests__/LongTasksAPI-itest.js index 6f2a06f2a9c..e3b8443676e 100644 --- a/packages/react-native/src/private/webapis/performance/__tests__/LongTasksAPI-itest.js +++ b/packages/react-native/src/private/webapis/performance/__tests__/LongTasksAPI-itest.js @@ -13,9 +13,6 @@ import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; import type {PerformanceObserverCallbackOptions} from '../PerformanceObserver'; import * as Fantom from '@react-native/fantom'; -import setUpPerformanceObserver from 'react-native/src/private/setup/setUpPerformanceObserver'; - -setUpPerformanceObserver(); function ensurePerformanceLongTaskTiming( value: mixed, diff --git a/packages/react-native/src/private/webapis/performance/__tests__/Performance-itest.js b/packages/react-native/src/private/webapis/performance/__tests__/Performance-itest.js index 32db3055f9c..4c8c5447744 100644 --- a/packages/react-native/src/private/webapis/performance/__tests__/Performance-itest.js +++ b/packages/react-native/src/private/webapis/performance/__tests__/Performance-itest.js @@ -10,10 +10,6 @@ import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; -import setUpPerformanceObserver from '../../../setup/setUpPerformanceObserver'; - -setUpPerformanceObserver(); - describe('Performance', () => { it('does NOT allow creating instances of Performance directly', () => { expect(() => { diff --git a/packages/react-native/src/private/webapis/performance/__tests__/PerformanceObserver-itest.js b/packages/react-native/src/private/webapis/performance/__tests__/PerformanceObserver-itest.js index ad3d38af930..41ddaa4be8f 100644 --- a/packages/react-native/src/private/webapis/performance/__tests__/PerformanceObserver-itest.js +++ b/packages/react-native/src/private/webapis/performance/__tests__/PerformanceObserver-itest.js @@ -10,12 +10,9 @@ import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; -import setUpPerformanceObserver from '../../../setup/setUpPerformanceObserver'; import {PerformanceObserverEntryList_public} from '../PerformanceObserver'; import * as Fantom from '@react-native/fantom'; -setUpPerformanceObserver(); - describe('PerformanceObserver', () => { it('receives notifications for marks and measures', () => { const callback = jest.fn(); diff --git a/packages/react-native/src/private/webapis/performance/__tests__/UserTiming-itest.js b/packages/react-native/src/private/webapis/performance/__tests__/UserTiming-itest.js index eb54e986f6e..99cb13114d4 100644 --- a/packages/react-native/src/private/webapis/performance/__tests__/UserTiming-itest.js +++ b/packages/react-native/src/private/webapis/performance/__tests__/UserTiming-itest.js @@ -11,12 +11,9 @@ import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; import ensureInstance from '../../../__tests__/utilities/ensureInstance'; -import setUpPerformanceObserver from '../../../setup/setUpPerformanceObserver'; import DOMException from '../../errors/DOMException'; import * as Fantom from '@react-native/fantom'; -setUpPerformanceObserver(); - function getThrownError(fn: () => mixed): mixed { try { fn();