React Profiler Component
TheProfiler component measures the performance of specific parts of your React application. It helps identify performance bottlenecks by tracking how long components take to render.
Basic Usage
Implementation Details
TheProfiler uses the REACT_PROFILER_TYPE symbol (see ReactClient.js:13) to identify profiler components in the React tree.
The onRender Callback
TheonRender callback receives six parameters providing detailed timing information:
Parameters
id: string
The unique identifier of the Profiler tree that committed. Use this to identify which part of your app was measured if you have multiple Profilers.
phase: “mount” | “update”
Indicates whether the component tree was mounted for the first time or re-rendered due to a prop, state, or hook change.
actualDuration: number
Time in milliseconds spent rendering the Profiler and its descendants for the current update. This indicates how well the subtree uses memoization.
Lower is better. A high value might indicate:
- Components aren’t memoized when they should be
- Expensive calculations aren’t using
useMemo - Too many components re-rendering unnecessarily
baseDuration: number
Time in milliseconds estimating how long it would take to render the entire Profiler subtree without any optimizations (without React.memo, useMemo, etc.).
Comparison:
- If
actualDurationis much lower thanbaseDuration, memoization is working well - If they’re similar, there’s little benefit from current memoization
startTime: number
Timestamp when React began rendering this update. Can be used to group renders from the same commit.
commitTime: number
Timestamp when React committed this update. Shared among all Profilers in a commit, enabling grouping.
Real-World Usage Patterns
Tracking Component Performance
FromReactProfiler-test.internal.js:287, here’s how to track mount and update performance:
Nested Profilers
Profilers can be nested to measure different parts of your app separately:- The
dashboardProfiler measures the entire dashboard including Header and Footer - The
sidebarandcontentProfilers provide more granular measurements - All will have the same
commitTimeif they update together
Conditional Profiling
Only profile in development or for specific users:Analyzing Performance Data
Collect and analyze performance data over time:Error Handling
FromReactProfiler-test.internal.js:68, errors thrown in onRender callbacks don’t break the commit phase:
Commit Phase Timing
FromReactProfiler-test.internal.js:104, the onRender callback is only invoked during the commit phase, not during render:
- Measurements reflect completed renders
- Interrupted renders (via Suspense) won’t trigger callbacks
- All lifecycle methods have already run