Sub topic 1
Why measure before you optimize
Performance work in React often starts with a feeling: the page seems slow, so you wrap components in memo and add useCallback everywhere. This chapter explains why that approach usually fails and what to do instead. It sets up the rest of the tutorial, which covers the tools and fixes in detail.
Prerequisites
This chapter assumes you can:
- Write JavaScript with functions, arrays, and closures.
- Build React function components that use props, state, and hooks.
- Open the browser developer tools and switch between panels.
You do not need any profiling experience. Later chapters introduce each tool as it is needed.
The cost of guessing
Optimizing without measuring has three common costs.
- You fix the wrong thing. The slow part of a page is often not where you expect. A component that looks heavy may render in under a millisecond. A small component may sit inside a loop and run thousands of times.
- You add complexity for no gain.
memo,useMemo, anduseCallbackall add code, dependency arrays, and places for bugs. A stale dependency array can show old data. If the optimization saves nothing, you still carry that risk. - You can make things slower. Memoization is not free. React must store the previous props or result and compare them on every render. If the comparison almost always fails, you pay for the check and still re-render.
The rule for this tutorial is simple: measure, change one thing, measure again.
What "slow" means
"Slow" is too vague to fix. Turn it into a statement you can test. Pick a specific user action and a number.
| Vague complaint | Testable statement |
|---|---|
| The search box feels laggy | Typing a character takes more than 100 ms to appear on screen |
| The dashboard is heavy | Opening the dashboard takes 3 seconds before the first chart shows |
| Scrolling is janky | Scrolling the table drops below 30 frames per second |
A rough guide for user-visible delay:
- Under about 16 ms per frame keeps animation smooth at 60 frames per second.
- Under about 100 ms feels instant for a click or keystroke.
- Over about 200 ms the user starts to notice a delay.
These are guidelines, not hard limits. The point is to choose a target before you start, so you know when to stop.
Where time goes in a React app
A slow interaction can be slow for several different reasons. Each one needs a different fix.
- React rendering. Your components run and React works out what changed. This is the main subject of this tutorial.
- Committing to the DOM. React applies the changes to the page. Large updates can be slow here, such as inserting thousands of nodes.
- Your own JavaScript. A heavy calculation, a large
filterorsort, or parsing a big response. - Network and loading. Large bundles, slow API calls, and many requests.
- Browser work after React. Style calculation, layout, and painting.
Only the first two are visible in React-specific tools. For the rest you need the browser's Performance panel, which a later chapter covers. If you assume every slowdown is a React rendering problem, you will spend hours memoizing components while the real cause is a 2 MB bundle.
A small example of a wrong guess
Here is a page that feels slow when you type. The tempting fix is to memoize the expensive component.
import { memo, useState } from "react";
const ExpensiveTree = memo(function ExpensiveTree({ data }) {
return (
<ul>
{data.map((d) => (
<li key={d.id}>{d.label}</li>
))}
</ul>
);
});
export default function Page() {
const [query, setQuery] = useState("");
// A new array is created on every render, so memo never skips.
const data = [{ id: 1, label: "One" }, { id: 2, label: "Two" }];
return (
<>
<input value={query} onChange={(e) => setQuery(e.target.value)} />
<ExpensiveTree data={data} />
</>
);
}This looks reasonable, but memo does nothing here. The data array is a new object on every render, so the shallow comparison of props always fails and ExpensiveTree re-renders anyway. The change added code and a comparison cost, and the page is no faster.
Had the developer profiled first, the profiler would have shown ExpensiveTree rendering on every keystroke even after the change. That evidence points to the real cause: unstable props. Later chapters show how to read that evidence.
A measurement workflow
Use the same loop for every optimization in this tutorial.
- Define the target. Choose one action and a number, as in the table above.
- Reproduce it reliably. Write down the exact steps. Use the same data and the same browser each time.
- Record a baseline. Measure the action before you change anything. Save the numbers.
- Find the cause. Use the profiler to see which components rendered and why.
- Make one change. Changing several things at once hides which one helped.
- Measure again. Compare against the baseline under the same conditions.
- Keep or revert. If the gain is small or zero, remove the change.
Making measurements trustworthy
A single number is weak evidence. Timings vary from run to run because of other tabs, browser caches, and background work. Follow these habits:
- Repeat the action several times and look at the typical result, not the best or worst one.
- Use a production build for final numbers. Development builds run extra checks and are slower, so their numbers do not match what users see. The next chapters cover this in detail.
- Test on slower hardware. Your development machine is probably faster than a typical user's phone. Browser developer tools can throttle the CPU to simulate a slower device.
- Keep conditions the same. Close other tabs, use the same data size, and measure the same interaction.
When not to optimize
Often the right answer is to do nothing. React is fast for most interfaces. If the interaction meets your target, stop. Time spent on a render that takes 2 ms is time not spent on something users will notice.
Be especially wary of these habits:
- Wrapping every component in
memoby default. - Adding
useMemoto cheap calculations such as adding two numbers. - Adding
useCallbackto functions that are not passed to memoized children or used as hook dependencies.
Memoize when the profiler shows a specific component rendering often with unchanged props, and rendering it is costly enough to matter.
What the rest of the tutorial covers
The next chapters follow the measurement loop. You first learn how React renders and commits, and how development and production builds differ. You then record sessions with the React DevTools Profiler and learn to read them. After that come the fixes: moving state down, memoization, virtualization, and deferring work. The last chapters cover measuring in code, the browser Performance panel, code splitting, and checking that gains last.
Exercise
A teammate says the product list is slow and wants to wrap every list item in memo right away. Describe the first three steps you would take instead, and give one reason memo might not help even after they add it.
Show answer
First, turn "slow" into a testable target, for example "filtering the list takes under 100 ms to update". Second, reproduce the action the same way each time and record a baseline measurement. Third, use the profiler to find which components render and why, before changing code. memo might not help if a prop changes identity on every render, such as an array, object, or function created inline in the parent. The shallow comparison then fails every time and the item re-renders anyway, so you pay for the comparison and gain nothing.
Exercise
You change one component and the measured time drops from 48 ms to 46 ms on a single run. Should you keep the change? What should you do next?
Show answer
Do not decide yet. A 2 ms difference on one run is within normal variation. Repeat the measurement several times under the same conditions, preferably on a production build, and compare typical results. If the gain is still small and the change adds complexity, revert it.