Question presented to candidate: "Forget how often a component renders. When one single render is slow, what do you do?"
What a strong answer should cover:
- Distinguish the two axes immediately: how often a component renders and how much one render costs. They have different fixes.
- Measure the second with the Profiler's
actualDuration, or theProfilercomponent'sonRendercallback. - What makes a single render expensive: heavy computation in the render body, creating very large element trees, deep prop drilling causing wide subtree renders, and expensive derived data.
- Move computation out of render:
useMemofor derived values, or compute it once outside the component if it does not depend on props. - Reduce the tree: virtualise long lists — the largest single win available, since it changes the element count by orders of magnitude.
- Split the component so the expensive part is isolated and can be skipped independently.
- Defer rather than shrink:
useTransitionanduseDeferredValuekeep input responsive while an expensive render happens at lower priority. - Know the difference between
actualDurationandbaseDuration— the latter is what it would cost with no memoisation. - Development builds are substantially slower; profile a production-profiling build.
Clarifying questions expected:
- "Is one render slow, or are there too many?" — that is the whole fork in this question.
- "How large is the tree we are rendering?"
Code / implementation expected: Yes — a Profiler measuring an expensive render, and the fixes applied.