The intermediate course showed memo, useMemo and transitions. Applied by
guesswork, they add complexity in the wrong places and miss the real problem.
Today starts with measuring: which components render, why, and how long
they take.
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Clear the console, then type two letters in Today's note.
What you'll see
Four lines: Note update: 0 ms and Chart update: 30 ms, twice. Each
keystroke re-renders the chart, and the chart costs 30 ms, almost two frames at
60 frames a second, to show the same five numbers.
<Profiler> measures everything inside it. After each commit, React calls
onRender with:
id, the name you gave it;
phase: "mount" the first time, "update" after that;
actualDuration: milliseconds spent rendering this subtree for this commit:
JavaScript
export function logRender(id, phase, actualDuration) { console.log(`${id} ${phase}: ${actualDuration.toFixed(0)} ms`);}
Wrap the main areas of a screen in profilers, use the app, and the console says
where the time goes. Profiler adds a little overhead, and is switched off in
production builds unless you opt in, so treat it as a development tool.
React Developer Tools
On your own computer, the React Developer Tools extension has a Profiler
tab. Press record, use the app, press stop. It shows every commit as a bar, and
every component's render time as a coloured block. Its settings include
Record why each component rendered, which answers the next question: was it
a state change, a parent render, or a context?
What to look for
Components that render when their output can't have changed, like the
chart here. The next lesson fixes this one.
One expensive component, rendering often. Make it cheaper, or render it
less.
Many cheap components, such as a long list. Render fewer of them: lesson 3.
Exercise
Profile the whole app
Wrap the entire <main> in a third profiler, App. Type a letter. How does
App's duration compare with Note's and Chart's together, and why?
Each keystroke logs App update: 30 ms or so, about the sum of the two inner
profilers, because App's subtree contains both. Nested profilers let you drill
down: start with the whole screen, then narrow to the part that's slow.
Completion is saved in this browser only. It does not measure mastery.