Require the NativePerformance module and drop the legacy performance fallback (#57566)

Summary:
Pull Request resolved: https://github.com/react/react-native/pull/57566

`setUpPerformance` previously branched between the modern Performance API setup and a barebones `performance` stub depending on whether the native Performance module was available. The native module is now always present in supported runtimes, so this consolidates everything on the modern setup and removes the dead fallback path.

- `NativePerformance` is now resolved via `getEnforcing` instead of a nullable `get`, and its `timeOrigin` method is no longer optional (the native module always implements it).
- Removed the legacy `performance` stub and the `global.nativePerformanceNow` fallbacks, along with the now-redundant `nullthrows`/null checks and the `getCurrentTimeStamp` indirection (native `now`/`timeOrigin` are cached directly).
- `Libraries/Core/setUpPerformance` is kept as a thin alias of the internal setup module so external callers importing it keep working; the internal module was renamed from `setUpPerformanceModern` to `setUpPerformance`.

Changelog: [Internal]

Reviewed By: hoxyq, cipolleschi

Differential Revision: D112116429

fbshipit-source-id: ffdca0cbf9104afbd0b2042c58834c6fdcd3bb92
This commit is contained in:
Rubén Norte authored and meta-codesync[bot] committed 2026-07-16 15:05:53 -07:00
1 parent e979b0ef8d
commit afc165129e
9 files changed
+33 -90

No files matched your search

+4 -21
View File
@@ -8,25 +8,8 @@
* @format
*/
import setUpPerformanceModern from '../../src/private/setup/setUpPerformanceModern';
import NativePerformance from '../../src/private/webapis/performance/specs/NativePerformance';
// Kept as an alias of the internal `setUpPerformance` module for external
// consumers that import this path directly.
import setUpPerformance from '../../src/private/setup/setUpPerformance';
// 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) {
setUpPerformanceModern();
} else {
if (!global.performance) {
// $FlowExpectedError[cannot-write]
global.performance = {
mark: () => {},
clearMarks: () => {},
measure: () => {},
clearMeasures: () => {},
now: () => {
const performanceNow = global.nativePerformanceNow || Date.now;
return performanceNow();
},
};
}
}
setUpPerformance();
@@ -10,7 +10,7 @@
let initialized = false;
export default function setUpDefaltReactNativeEnvironment(
export default function setUpDefaultReactNativeEnvironment(
enableDeveloperTools: boolean = true,
) {
if (initialized) {
@@ -21,7 +21,7 @@ export default function setUpDefaltReactNativeEnvironment(
require('../../../Libraries/Core/setUpGlobals');
require('./setUpDOM').default();
require('../../../Libraries/Core/setUpPerformance');
require('./setUpPerformance').default();
require('../../../Libraries/Core/polyfillPromise');
require('../../../Libraries/Core/setUpTimers');
if (__DEV__ && enableDeveloperTools) {
@@ -12,7 +12,7 @@ import {polyfillGlobal} from '../../../Libraries/Utilities/PolyfillFunctions';
let initialized = false;
export default function setUpPerformanceModern() {
export default function setUpPerformance() {
if (initialized) {
return;
}
@@ -16,10 +16,7 @@ import type {
} from './PerformanceEntry';
import {PerformanceEntry} from './PerformanceEntry';
import MaybeNativePerformance from './specs/NativePerformance';
import nullthrows from 'nullthrows';
const NativePerformance = nullthrows(MaybeNativePerformance);
import NativePerformance from './specs/NativePerformance';
export type PerformanceEventTimingJSON = {
...PerformanceEntryJSON,
@@ -29,12 +29,10 @@ import {
performanceEntryTypeToRaw,
rawToPerformanceEntry,
} from './internals/RawPerformanceEntry';
import {getCurrentTimeStamp} from './internals/Utilities';
import MemoryInfo from './MemoryInfo';
import ReactNativeStartupTiming from './ReactNativeStartupTiming';
import MaybeNativePerformance from './specs/NativePerformance';
import NativePerformance from './specs/NativePerformance';
import {PerformanceMark, PerformanceMeasure} from './UserTiming';
import nullthrows from 'nullthrows';
export type PerformanceMeasureOptions =
| Readonly<{
@@ -56,13 +54,8 @@ export type PerformanceMeasureOptions =
const ENTRY_TYPES_AVAILABLE_FROM_TIMELINE: ReadonlyArray<PerformanceEntryType> =
['mark', 'measure'];
const NativePerformance = nullthrows(MaybeNativePerformance);
const cachedReportMark = NativePerformance.reportMark;
const cachedReportMeasure = NativePerformance.reportMeasure;
const cachedGetMarkTime = NativePerformance.getMarkTime;
const cachedNativeClearMarks = NativePerformance.clearMarks;
const cachedNativeClearMeasures = NativePerformance.clearMeasures;
const {now, reportMark, reportMeasure, getMarkTime, clearMarks, clearMeasures} =
NativePerformance;
let cachedTimeOrigin: ?DOMHighResTimeStamp;
const MARK_OPTIONS_REUSABLE_OBJECT: PerformanceMarkOptions = {
@@ -78,7 +71,7 @@ const MEASURE_OPTIONS_REUSABLE_OBJECT: PerformanceMeasureInit = {
};
const getMarkTimeForMeasure = (markName: string): number => {
const markTime = cachedGetMarkTime(markName);
const markTime = getMarkTime(markName);
if (markTime == null) {
throw new DOMException(
`Failed to execute 'measure' on 'Performance': The mark '${markName}' does not exist.`,
@@ -147,12 +140,7 @@ export default class Performance {
*/
get timeOrigin(): DOMHighResTimeStamp {
if (cachedTimeOrigin == null) {
if (NativePerformance.timeOrigin) {
cachedTimeOrigin = NativePerformance?.timeOrigin();
} else {
// Very naive polyfill.
cachedTimeOrigin = Date.now() - getCurrentTimeStamp();
}
cachedTimeOrigin = NativePerformance.timeOrigin();
}
return cachedTimeOrigin;
@@ -202,7 +190,7 @@ export default class Performance {
);
}
} else {
resolvedStartTime = getCurrentTimeStamp();
resolvedStartTime = now();
}
if (detail !== undefined) {
@@ -219,13 +207,13 @@ export default class Performance {
MARK_OPTIONS_REUSABLE_OBJECT,
);
cachedReportMark(resolvedMarkName, resolvedStartTime, entry);
reportMark(resolvedMarkName, resolvedStartTime, entry);
return entry;
}
clearMarks(markName?: string): void {
cachedNativeClearMarks(markName);
clearMarks(markName);
}
measure(
@@ -347,7 +335,7 @@ export default class Performance {
) {
resolvedDuration = resolvedEndTime - resolvedStartTime;
} else {
resolvedDuration = getCurrentTimeStamp() - resolvedStartTime;
resolvedDuration = now() - resolvedStartTime;
}
}
@@ -364,7 +352,7 @@ export default class Performance {
resolvedDuration =
getMarkTimeForMeasure(endMark) - resolvedStartTime;
} else {
resolvedDuration = getCurrentTimeStamp() - resolvedStartTime;
resolvedDuration = now() - resolvedStartTime;
}
break;
}
@@ -375,7 +363,7 @@ export default class Performance {
resolvedDuration =
getMarkTimeForMeasure(endMark) - resolvedStartTime;
} else {
resolvedDuration = getCurrentTimeStamp() - resolvedStartTime;
resolvedDuration = now() - resolvedStartTime;
}
}
}
@@ -385,7 +373,7 @@ export default class Performance {
if (endMark !== undefined) {
resolvedDuration = getMarkTimeForMeasure(endMark) - resolvedStartTime;
} else {
resolvedDuration = getCurrentTimeStamp() - resolvedStartTime;
resolvedDuration = now() - resolvedStartTime;
}
}
@@ -400,7 +388,7 @@ export default class Performance {
const entry = new PerformanceMeasure(MEASURE_OPTIONS_REUSABLE_OBJECT);
cachedReportMeasure(
reportMeasure(
resolvedMeasureName,
resolvedStartTime,
resolvedDuration,
@@ -411,14 +399,14 @@ export default class Performance {
}
clearMeasures(measureName?: string): void {
cachedNativeClearMeasures(measureName);
clearMeasures(measureName);
}
/**
* Returns a double, measured in milliseconds.
* https://developer.mozilla.org/en-US/docs/Web/API/Performance/now
*/
now: () => DOMHighResTimeStamp = getCurrentTimeStamp;
now: () => DOMHighResTimeStamp = now;
/**
* An extension that allows to get back to JS all currently logged marks/measures
@@ -21,13 +21,11 @@ import {
rawToPerformanceEntry,
rawToPerformanceEntryType,
} from './internals/RawPerformanceEntry';
import MaybeNativePerformance from './specs/NativePerformance';
import NativePerformance from './specs/NativePerformance';
import nullthrows from 'nullthrows';
export {PerformanceEntry} from './PerformanceEntry';
const NativePerformance = nullthrows(MaybeNativePerformance);
export class PerformanceObserverEntryList {
#entries: PerformanceEntryList;
@@ -18,8 +18,10 @@ import type {
ExtensionTrackEntryPayload,
} from './UserTimingExtensibility';
import {getCurrentTimeStamp} from './internals/Utilities';
import {PerformanceEntry} from './PerformanceEntry';
import NativePerformance from './specs/NativePerformance';
const {now} = NativePerformance;
export type DetailType =
| unknown
@@ -47,7 +49,7 @@ class PerformanceMarkTemplate extends PerformanceEntry {
constructor(markName: string, markOptions?: PerformanceMarkOptions) {
super('mark', {
name: markName,
startTime: markOptions?.startTime ?? getCurrentTimeStamp(),
startTime: markOptions?.startTime ?? now(),
duration: 0,
});
@@ -73,7 +75,7 @@ export const PerformanceMark: typeof PerformanceMarkTemplate =
) {
this.__entryType = 'mark';
this.__name = markName;
this.__startTime = markOptions?.startTime ?? getCurrentTimeStamp();
this.__startTime = markOptions?.startTime ?? now();
this.__duration = 0;
this.__detail = markOptions?.detail ?? null;
@@ -1,27 +0,0 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @format
*/
import warnOnce from '../../../../../Libraries/Utilities/warnOnce';
import NativePerformance from '../specs/NativePerformance';
export function warnNoNativePerformance() {
warnOnce(
'missing-native-performance',
'Missing native implementation of Performance',
);
}
declare var global: {
// This value is defined directly via JSI, if available.
readonly nativePerformanceNow?: ?() => number,
};
export const getCurrentTimeStamp: () => DOMHighResTimeStamp =
NativePerformance?.now ?? global.nativePerformanceNow ?? (() => Date.now());
@@ -57,7 +57,7 @@ export type PerformanceObserverInit = {
export interface Spec extends TurboModule {
readonly now: () => number;
readonly timeOrigin?: () => number;
readonly timeOrigin: () => number;
readonly reportMark: (
name: string,
@@ -107,4 +107,6 @@ export interface Spec extends TurboModule {
readonly clearEventCountsForTesting: () => void;
}
export default TurboModuleRegistry.get<Spec>('NativePerformanceCxx') as ?Spec;
export default TurboModuleRegistry.getEnforcing<Spec>(
'NativePerformanceCxx',
) as Spec;