@callstack/reassure-measure
Version:
Performance measurement library for React and React Native
38 lines (36 loc) • 1.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.applyRenderPolyfills = applyRenderPolyfills;
exports.revertRenderPolyfills = revertRenderPolyfills;
var _perf_hooks = require("perf_hooks");
var _testingLibrary = require("./testing-library");
function applyRenderPolyfills() {
const testingLibrary = (0, _testingLibrary.getTestingLibrary)();
if (testingLibrary === 'react-native') {
polyfillPerformanceNow();
}
}
function revertRenderPolyfills() {
const testingLibrary = (0, _testingLibrary.getTestingLibrary)();
if (testingLibrary === 'react-native') {
restorePerformanceNow();
}
}
/**
* React Native Jest preset mocks the global.performance object, with `now()` method being `Date.now()`.
* Ref: https://github.com/facebook/react-native/blob/3dfe22bd27429a43b4648c597b71f7965f31ca65/packages/react-native/jest/setup.js#L41
*
* Then React uses `performance.now()` in `Scheduler` to measure component render time.
* https://github.com/facebook/react/blob/45804af18d589fd2c181f3b020f07661c46b73ea/packages/scheduler/src/forks/Scheduler.js#L59
*/
let originalPerformanceNow;
function polyfillPerformanceNow() {
originalPerformanceNow = global.performance?.now;
global.performance.now = () => _perf_hooks.performance.now();
}
function restorePerformanceNow() {
globalThis.performance.now = originalPerformanceNow;
}
//# sourceMappingURL=polyfills.js.map