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
This commit is contained in:
Rubén Norte
2025-09-08 14:44:39 +01:00
committed by Vitali Zaidman
parent a65325b586
commit cdfe1ecd52
7 changed files with 11 additions and 22 deletions
+3 -5
View File
@@ -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]
@@ -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,
@@ -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) {}
@@ -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,
@@ -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(() => {
@@ -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();
@@ -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();