Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix tests
  • Loading branch information
jorge-cab committed May 27, 2025
commit 6c71a7766d4ca9806f6b6842a904d0bd8159c25d
1 change: 1 addition & 0 deletions packages/react-devtools-extensions/webpack.backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ module.exports = {
__IS_FIREFOX__: IS_FIREFOX,
__IS_EDGE__: IS_EDGE,
__IS_NATIVE__: false,
__IS_INTERNAL_MCP_BUILD__: false,
}),
new Webpack.SourceMapDevToolPlugin({
filename: '[file].map',
Expand Down
154 changes: 152 additions & 2 deletions packages/react-devtools-shared/src/backend/fiber/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5113,6 +5113,151 @@
let rootToCommitProfilingMetadataMap: CommitProfilingMetadataMap | null =
null;

function printPerformanceData(profilingData: ProfilingDataBackend): void {
if (
!profilingData ||
!profilingData.dataForRoots ||
profilingData.dataForRoots.length === 0
) {
console.log('No profiling data available');
return;
}

console.group('React Performance Data');

profilingData.dataForRoots.forEach(rootData => {
const {commitData, displayName, initialTreeBaseDurations} = rootData;

Check failure on line 5129 in packages/react-devtools-shared/src/backend/fiber/renderer.js

View workflow job for this annotation

GitHub Actions / Run eslint

'initialTreeBaseDurations' is assigned a value but never used

console.group(`Root: ${displayName}`);

// Print overall stats
const totalCommits = commitData.length;
const totalCommitDuration = commitData.reduce(
(sum, commit) => sum + commit.duration,
0,
);
const avgCommitDuration = totalCommitDuration / totalCommits;

console.log(`Total commits: ${totalCommits}`);
console.log(`Average commit duration: ${avgCommitDuration.toFixed(2)}ms`);

// Find slow components (components that took a long time to render)
const componentDurations = new Map();

commitData.forEach(commit => {
if (commit.fiberActualDurations) {
commit.fiberActualDurations.forEach(([fiberID, duration]) => {
if (!componentDurations.has(fiberID)) {
componentDurations.set(fiberID, []);
}
componentDurations.get(fiberID).push(duration);
});
}
});

// Calculate average render times for each component
const averageComponentDurations = new Map();
componentDurations.forEach((durations, fiberID) => {
const total = durations.reduce((sum, duration) => sum + duration, 0);
const average = total / durations.length;
averageComponentDurations.set(fiberID, {
average,
count: durations.length,
total,
});
});

// Sort components by average render time (descending)
const sortedComponents = Array.from(
averageComponentDurations.entries(),
).sort((a, b) => b[1].average - a[1].average);

// Print the top 10 slowest components
console.group('Top 10 slowest components (by average render time)');
sortedComponents
.slice(0, 10)
.forEach(([fiberID, {average, count, total}]) => {
// Try to find component name from snapshots
let componentName = `FiberID: ${fiberID}`;
if (rootData.snapshots) {
const snapshot = rootData.snapshots.find(
snap => snap[0] === fiberID,
);
if (snapshot && snapshot[1] && snapshot[1].displayName) {
componentName = snapshot[1].displayName;
}
}

console.log(
`${componentName}: ${average.toFixed(2)}ms avg (${count} renders, ${total.toFixed(2)}ms total)`,
);
});
console.groupEnd();

// Print components with most renders
const sortedByRenderCount = Array.from(
averageComponentDurations.entries(),
).sort((a, b) => b[1].count - a[1].count);

console.group('Top 10 most frequently rendered components');
sortedByRenderCount
.slice(0, 10)
.forEach(([fiberID, {average, count, total}]) => {
// Try to find component name from snapshots
let componentName = `FiberID: ${fiberID}`;
if (rootData.snapshots) {
const snapshot = rootData.snapshots.find(
snap => snap[0] === fiberID,
);
if (snapshot && snapshot[1] && snapshot[1].displayName) {
componentName = snapshot[1].displayName;
}
}

console.log(
`${componentName}: ${count} renders (${average.toFixed(2)}ms avg, ${total.toFixed(2)}ms total)`,
);
});
console.groupEnd();

// Print effect durations if available
const effectDurations = commitData
.filter(commit => commit.effectDuration !== null)
.map(commit => commit.effectDuration);

const passiveEffectDurations = commitData
.filter(commit => commit.passiveEffectDuration !== null)
.map(commit => commit.passiveEffectDuration);

if (effectDurations.length > 0) {
const totalEffectDuration = effectDurations.reduce(
(sum, duration) => sum + duration,
0,
);
const avgEffectDuration = totalEffectDuration / effectDurations.length;
console.log(
`Average effect duration: ${avgEffectDuration.toFixed(2)}ms`,
);
}

if (passiveEffectDurations.length > 0) {
const totalPassiveEffectDuration = passiveEffectDurations.reduce(
(sum, duration) => sum + duration,
0,
);
const avgPassiveEffectDuration =
totalPassiveEffectDuration / passiveEffectDurations.length;
console.log(
`Average passive effect duration: ${avgPassiveEffectDuration.toFixed(2)}ms`,
);
}

console.groupEnd(); // Root group
});

console.groupEnd(); // React Performance Data group
}

function getProfilingData(): ProfilingDataBackend {
const dataForRoots: Array<ProfilingDataForRootBackend> = [];

Expand Down Expand Up @@ -5205,7 +5350,7 @@
...rest
} = currentTimelineData;

timelineData = {

Check failure on line 5353 in packages/react-devtools-shared/src/backend/fiber/renderer.js

View workflow job for this annotation

GitHub Actions / Run eslint

'timelineData' is assigned a value but never used
...rest,

// Most of the data is safe to parse as-is,
Expand All @@ -5226,11 +5371,16 @@
}
}

return {
const profilingData = {
dataForRoots,
rendererID,
timelineData,
timelineData: getTimelineData?.() || null,
};

// Print the performance data to help with debugging
printPerformanceData(profilingData);

return profilingData;
}

function snapshotTreeBaseDurations(
Expand Down
1 change: 1 addition & 0 deletions scripts/jest/devtools/setupEnv.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ global.__IS_FIREFOX__ = false;
global.__IS_CHROME__ = false;
global.__IS_EDGE__ = false;
global.__IS_NATIVE__ = false;
global.__IS_INTERNAL_MCP_BUILD__ = false;

const ReactVersionTestingAgainst = process.env.REACT_VERSION || ReactVersion;

Expand Down
Loading