Created
December 28, 2021 13:46
-
-
Save enigma1/b86cd2ea1579634ff22dad6aa1bf175b to your computer and use it in GitHub Desktop.
Simple profiling of a react component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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