{"componentChunkName":"component---src-templates-docs-js","path":"/docs/profiler.html","result":{"data":{"markdownRemark":{"html":"
\n\nThese docs are old and won’t be updated. Go to react.dev for the new React docs.
\nThese new documentation pages teach modern React:
\n\n
\n- \n
<Profiler>
The Profiler measures how often a React application renders and what the “cost” of rendering is.\nIts purpose is to help identify parts of an application that are slow and may benefit from optimizations such as memoization.
\n\nNote:
\nProfiling adds some additional overhead, so it is disabled in the production build.
\nTo opt into production profiling, React provides a special production build with profiling enabled.\nRead more about how to use this build at fb.me/react-profiling
\n
A Profiler can be added anywhere in a React tree to measure the cost of rendering that part of the tree.\nIt requires two props: an id (string) and an onRender callback (function) which React calls any time a component within the tree “commits” an update.
For example, to profile a Navigation component and its descendants:
render(\n <App>\n <Profiler id=\"Navigation\" onRender={callback}> <Navigation {...props} />\n </Profiler>\n <Main {...props} />\n </App>\n);Multiple Profiler components can be used to measure different parts of an application:
render(\n <App>\n <Profiler id=\"Navigation\" onRender={callback}> <Navigation {...props} />\n </Profiler>\n <Profiler id=\"Main\" onRender={callback}> <Main {...props} />\n </Profiler>\n </App>\n);Profiler components can also be nested to measure different components within the same subtree:
render(\n <App>\n <Profiler id=\"Panel\" onRender={callback}> <Panel {...props}>\n <Profiler id=\"Content\" onRender={callback}> <Content {...props} />\n </Profiler>\n <Profiler id=\"PreviewPane\" onRender={callback}> <PreviewPane {...props} />\n </Profiler>\n </Panel>\n </Profiler>\n </App>\n);\n\nNote
\nAlthough
\nProfileris a light-weight component, it should be used only when necessary; each use adds some CPU and memory overhead to an application.
onRender Callback The Profiler requires an onRender function as a prop.\nReact calls this function any time a component within the profiled tree “commits” an update.\nIt receives parameters describing what was rendered and how long it took.
function onRenderCallback(\n id, // the \"id\" prop of the Profiler tree that has just committed\n phase, // either \"mount\" (if the tree just mounted) or \"update\" (if it re-rendered)\n actualDuration, // time spent rendering the committed update\n baseDuration, // estimated time to render the entire subtree without memoization\n startTime, // when React began rendering this update\n commitTime, // when React committed this update\n interactions // the Set of interactions belonging to this update\n) {\n // Aggregate or log render timings...\n}Let’s take a closer look at each of the props:
\nid: string -\nThe id prop of the Profiler tree that has just committed.\nThis can be used to identify which part of the tree was committed if you are using multiple profilers.phase: \"mount\" | \"update\" -\nIdentifies whether the tree has just been mounted for the first time or re-rendered due to a change in props, state, or hooks.actualDuration: number -\nTime spent rendering the Profiler and its descendants for the current update.\nThis indicates how well the subtree makes use of memoization (e.g. React.memo, useMemo, shouldComponentUpdate).\nIdeally this value should decrease significantly after the initial mount as many of the descendants will only need to re-render if their specific props change.baseDuration: number -\nDuration of the most recent render time for each individual component within the Profiler tree.\nThis value estimates a worst-case cost of rendering (e.g. the initial mount or a tree with no memoization).startTime: number -\nTimestamp when React began rendering the current update.commitTime: number -\nTimestamp when React committed the current update.\nThis value is shared between all profilers in a commit, enabling them to be grouped if desirable.interactions: Set -\nSet of “interactions” that were being traced when the update was scheduled (e.g. when render or setState were called).\n","frontmatter":{"title":"Profiler API","next":null,"prev":null},"fields":{"path":"content/docs/reference-profiler.md","slug":"docs/profiler.html"}}},"pageContext":{"slug":"docs/profiler.html"}},"staticQueryHashes":[]}Note
\nInteractions can be used to identify the cause of an update, although the API for tracing them is still experimental.
\nLearn more about it at fb.me/react-interaction-tracing
\n