Sub topic 15
Verifying improvements and guarding against regressions
This chapter assumes you can record a React DevTools Profiler session, read the flamegraph and ranked charts, and apply fixes such as state colocation, memo, useMemo, and useCallback. Here you confirm that a fix helped and keep it from silently breaking later.
Measure the same thing twice
A fix is only proven by a before-and-after comparison taken under the same conditions. Differences in conditions can easily hide or fake an improvement.
- Use the same build. Compare production-profiling builds with production-profiling builds, never development with production.
- Repeat the same interaction, for example typing five characters into the search box or opening the same panel.
- Keep the data size, browser, and CPU throttling setting the same.
- Record several runs. Single timings vary, so compare the typical value and not the best one.
Record what you compare. For each run, note the commit count, the total time of the commits, and the render time of the component you changed.
| Measure | Before | After |
|---|---|---|
| Commits during interaction | 5 | 5 |
| Slowest commit | 84 ms | 11 ms |
| `ExpensiveTree` renders | 5 | 0 |
The last row is the most reliable signal. A render count that drops to zero is evidence that does not depend on machine speed. Timings are noisier.
Check the cause, not only the number
After a fix, open the Profiler again and use the "Why did this render?" information on the component you targeted. If you expected ExpensiveTree to stop re-rendering, it should now be grey (did not render) in the flamegraph for those commits. If it still renders, the cause you removed was not the real one.
A common surprise is a memoized component that still re-renders. The usual reason is a prop that changes identity on every render, such as an inline object or function. Fix the prop, then measure again.
Automate a check with the Profiler component
The Profiler component can collect numbers in a test or a development session, so a regression shows up as a failing assertion and not as a vague feeling that the app is slow. This example counts commits and fails if a typing interaction causes more renders of the tree than expected.
import { Profiler } from "react";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { test, expect } from "vitest";
import { Page } from "./Page";
test("typing does not re-render the expensive tree", async () => {
const commits = [];
const onRender = (id, phase) => commits.push({ id, phase });
render(
<Profiler id="Tree" onRender={onRender}>
<Page />
</Profiler>
);
const mounts = commits.length;
await userEvent.type(screen.getByRole("textbox"), "abc");
// Only the mount should have reached the tree's Profiler.
expect(commits.length).toBe(mounts);
});Here the Profiler wraps only the part that must stay quiet. If typing reaches the tree, onRender fires and the assertion fails. Assert on render counts, not on milliseconds. Durations in a test runner depend on the machine and make tests flaky.
Note that Profiler does nothing useful in standard production builds. It is active in development and in profiling builds, so run this kind of check in your test environment.
Guard against regressions
Practical guards, from cheapest to most expensive:
- Keep a comment next to non-obvious memoization saying what it protects, so nobody removes it by accident.
- Add render-count tests like the one above for the few components that matter most.
- Track bundle size in your build and fail the build when it grows past a limit. This covers the code-splitting work.
- Re-profile after large features land, especially ones that add shared state or context.
Exercise
You wrapped a list row in memo and the Profiler shows the same number of row renders as before. What do you check first, and how do you confirm the fix once it works?
Show answer
Check whether a prop changes identity on every render, such as an inline function or object, using the "Why did this render?" information. Stabilize it with useCallback or useMemo, then repeat the same interaction in the same build and confirm the row render count dropped. Optionally add a render-count test so it stays fixed.