Skip to content

Instantly share code, notes, and snippets.

@enigma1
Created December 28, 2021 13:46
Show Gist options
  • Select an option

  • Save enigma1/b86cd2ea1579634ff22dad6aa1bf175b to your computer and use it in GitHub Desktop.

Select an option

Save enigma1/b86cd2ea1579634ff22dad6aa1bf175b to your computer and use it in GitHub Desktop.
Simple profiling of a react component
// Simple react profiling code to wrap components into react profiler
// Event timings are shown via a console statement of the profiler's callback
// Below the list of the documented arguments passed to the callback as shown in React docs
// ----------
// 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
// -----------
import {Profiler} from 'react';
const getTimings = (...args) => console.log(args);
const profile = (Component) => props =>
<Profiler id={Component.name} onRender={getTimings}>
<Component {...props} />
</Profiler>
const MyComponent = () => <div>My Component</div>
export default profile(MyComponent);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment