Skip to content

Commit

Permalink
refactor: improve
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjastrzebski committed Oct 21, 2024
1 parent 4cf62fb commit 919b01f
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions packages/measure/src/measure-renders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,15 @@ async function measureRendersInternal(
const testingLibrary = getTestingLibrary();

showFlagsOutputIfNeeded();
ensurePreciseMeasurements();

const runResults: RunResult[] = [];
let hasTooLateRender = false;

const renderJsonTrees: ElementJsonTree[] = [];
let initialRenderCount = 0;

installPerformanceNow();

for (let i = 0; i < runs + warmupRuns; i += 1) {
let duration = 0;
let count = 0;
Expand Down Expand Up @@ -119,6 +120,8 @@ async function measureRendersInternal(
runResults.push({ duration, count });
}

restorePerformanceNow();

if (hasTooLateRender) {
const testName = expect.getState().currentTestName;
logger.warn(
Expand Down Expand Up @@ -149,7 +152,18 @@ export function buildUiToRender(
return Wrapper ? <Wrapper>{uiWithProfiler}</Wrapper> : uiWithProfiler;
}

let originalNow: () => number;

//https://github.com/facebook/react/blob/65a56d0e99261481c721334a3ec4561d173594cd/packages/react-devtools-shared/src/backend/fiber/renderer.js#L294
function ensurePreciseMeasurements() {
function installPerformanceNow() {
originalNow = globalThis.performance.now;
globalThis.performance.now = () => perf.now();
}

function restorePerformanceNow() {
if (originalNow == null) {
throw new Error('Called "restorePerformanceNow" without calling "installPerformanceNow" first');
}

globalThis.performance.now = originalNow;
}

0 comments on commit 919b01f

Please sign in to comment.