Skip to main content

Profiler

Check out the official React docs!

For more detailed information about React concepts and APIs, please refer to the official documentation for Profiler.


React.Profiler measures how often a part of the tree renders and how long that takes. React calls onRender after every commit of the wrapped tree with a ProfilerRenderInfo:

FieldMeaning
idThe id you passed to the Profiler.
phase"mount", "update" or "nested-update".
actualDurationMilliseconds spent rendering the tree for this update.
baseDurationEstimated milliseconds to re-render the whole tree without memoization.
startTime / commitTimeWhen React started rendering and committed this update.
React.Profiler(
"sidebar",
(fun info -> console.log (info.id, info.phase, info.actualDuration)),
[ Sidebar() ]
)
note

Profiling adds overhead, so onRender is only called in development builds (and in React's special profiling build). That's also why there is no live demo on this production site.

Don't update state from onRender: it runs after every commit, so that would loop.

module Example.Profiler

open Feliz

[<ReactComponent>]
let Counter () =
let count, setCount = React.useState 0
Html.button [ prop.onClick (fun _ -> setCount (count + 1)); prop.text $"Clicked {count} times" ]

[<ReactComponent(true)>]
let Profiler () =
let lastCommit = React.useRef "nothing measured yet"
let report, setReport = React.useState ""

Html.div [
React.Profiler(
"counter",
(fun info ->
// Don't set state here: onRender runs after every commit, so that would loop.
lastCommit.current <- $"{info.phase} took {System.Math.Round(info.actualDuration, 2)} ms"
),
[ Counter() ]
)
Html.button [ prop.onClick (fun _ -> setReport lastCommit.current); prop.text "Show last commit" ]
Html.p report
]