Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Profiling React app in Chrome DevTools - Profiling not supported

When i try to profile the production build of my app, in the Profile tab of the Chrome DevTools, i get the following message:

Profiling not supported.
Profiling support requires either a development or production-profiling build of React v16.5+.

(my React version is 16.8) How can i fix that?

I'm using Webpack4, and adding this to my config file as suggested by the official React docs didn't help:

  resolve: {
    alias: {
      'react-dom$': 'react-dom/profiling',
      'scheduler/tracing': 'scheduler/tracing-profiling',
    }
  }
};
like image 351
Gambit2007 Avatar asked Jan 01 '20 18:01

Gambit2007


People also ask

How do you profiling in React JS?

Usage. A Profiler can be added anywhere in a React tree to measure the cost of rendering that part of the tree. It requires two props: an id (string) and an onRender callback (function) which React calls any time a component within the tree “commits” an update.

How do I turn on React Profiler?

Open the browser developer tools by right clicking anywhere on the page and clicking "Inspect." Then select the "⚛ Profiler" tab. This is the React DevTools profiler and you can now click the little blue circle to "Start profiling" the application.


1 Answers

By default, React doesn't include profiling in production builds, you'll need to add that by using the Profiler API.

The Profiler API provides a component which takes an id (string), and an onRender callback as arguments. You can wrap any part of your React tree with this component and get profiling info from it, though it won't be available in the DevTools, and thus won't have the nice flame chart. It would look something like this:

<Profiler id="MyComponent" onRender={onRenderCallback}>
  <MyComponent {...props} />
</Profiler>

The onRender callback does provide some neat info though that you could send off to an API endpoint:

function onRenderCallback(
  id, // the "id" prop of the Profiler tree that has just committed
  phase, // either "mount" (if the tree just mounted) or "update" (if it re-rendered)
  actualDuration, // time spent rendering the committed update
  baseDuration, // estimated time to render the entire subtree without memoization
  startTime, // when React began rendering this update
  commitTime, // when React committed this update
  interactions // the Set of interactions belonging to this update
) {
  // Aggregate or log render timings...
}

This ☝️ came directly from the React docs linked above.

Keep in mind that any component you add, adds CPU and memory overhead, so you should really only use the React.Profiler when needed. Ideally you should be profiling your components and finding bottlenecks in development mode.

It's worth noting that the config you included above for 'react-dom/profiling' and 'scheduler/tracing-profiling' is required during your build process to get this React.Profiler working.

Hope this helps you out!

like image 60
lauchness Avatar answered Oct 11 '22 17:10

lauchness