diff --git a/Libraries/Core/setUpPerformance.js b/Libraries/Core/setUpPerformance.js index ef12f6c9ae1..c5f25890cdc 100644 --- a/Libraries/Core/setUpPerformance.js +++ b/Libraries/Core/setUpPerformance.js @@ -8,18 +8,29 @@ * @format */ -if (!global.performance) { - global.performance = ({}: {now?: () => number}); -} +import NativePerformance from '../WebPerformance/NativePerformance'; +import Performance from '../WebPerformance/Performance'; -/** - * Returns a double, measured in milliseconds. - * https://developer.mozilla.org/en-US/docs/Web/API/Performance/now - */ -if (typeof global.performance.now !== 'function') { - // $FlowExpectedError[cannot-write] The global isn't writable anywhere but here, where we define it. - global.performance.now = function () { - const performanceNow = global.nativePerformanceNow || Date.now; - return performanceNow(); - }; +// 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) { + // $FlowExpectedError[cannot-write] + global.performance = new Performance(); +} else { + if (!global.performance) { + // $FlowExpectedError[cannot-write] + global.performance = ({}: {now?: () => number}); + } + + /** + * Returns a double, measured in milliseconds. + * https://developer.mozilla.org/en-US/docs/Web/API/Performance/now + */ + if (typeof global.performance.now !== 'function') { + // $FlowExpectedError[cannot-write] + global.performance.now = function () { + const performanceNow = global.nativePerformanceNow || Date.now; + return performanceNow(); + }; + } } diff --git a/Libraries/Core/setUpWebPerformance.js b/Libraries/Core/setUpWebPerformance.js deleted file mode 100644 index 333c5c57c1b..00000000000 --- a/Libraries/Core/setUpWebPerformance.js +++ /dev/null @@ -1,17 +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 Performance from '../WebPerformance/Performance'; - -// TODO: Replace setUpPerformance with this once the WebPerformance API is stable (T143070419) -export default function setUpPerformance() { - // $FlowExpectedError[cannot-write] The global isn't writable anywhere but here, where we define it. - global.performance = new Performance(); -}