Sub topic 1
Why Measure Before You Optimize
This chapter explains why profiling comes before any performance fix in a React app. It also introduces the workflow the rest of the tutorial follows.
Prerequisites
This chapter assumes you can:
- Write JavaScript functions, work with arrays, and use closures.
- Build React function components with props, state, and hooks.
- Open the browser developer tools and switch between panels.
You do not need any profiling experience.
The problem with guessing
Most React code is fast enough. When an app does feel slow, the cause is often not where developers expect it. A common pattern is to wrap components in React.memo, add useCallback everywhere, and hope. This has costs:
- Memoization adds code, and every dependency array is something that can go stale and cause bugs.
- Memoization does work too. React must compare props on every render, and that comparison is wasted if the props change anyway.
- If the real cause is a large bundle or a long network request, no render fix will help.
An optimization that is not tied to a measured problem adds complexity and may not change anything the user can feel.
What "slow" means
"Slow" needs a definition before you can measure it. Pick a specific interaction and a symptom:
- Typing in a search box lags behind the keys.
- Switching tabs freezes the page for half a second.
- The first load shows a blank screen for several seconds.
Each symptom points at a different place. Input lag usually points at rendering work during an update. A blank first load often points at bundle size or network time. The symptom tells you which tool to open first.
The measure-change-measure loop
The workflow used throughout this tutorial has four steps.
- Measure. Record the interaction and note a number, such as "update takes 180 ms".
- Find the cause. Identify which components render, how long they take, and why they rendered.
- Apply one fix. Change one thing, so you know which change had the effect.
- Measure again. Repeat the same interaction and compare with the first number.
The loop ends when the interaction is fast enough, not when the code is as optimized as possible.
Measuring fairly
A comparison is only useful if both measurements are taken under the same conditions. Keep these constant:
| Factor | Why it matters |
|---|---|
| Same interaction | A different click path renders different components. |
| Same data size | Rendering cost often grows with the number of items. |
| Same build type | Development builds are slower than production builds. |
| Same device and load | Other browser tabs and CPU throttling change timings. |
Run each measurement several times. Single runs vary, so look at the typical value and not the best or worst one.
The build type deserves special care. React's development build does extra checks, so its timings are larger than what users see. A later chapter covers this in detail.
Where the time goes
Not every slow page is a slow render. Time can be spent in several places:
- Network: downloading JavaScript, data, and images.
- Scripting outside React: your own heavy calculations or third-party scripts.
- React rendering: calling your components to work out what changed.
- Committing and painting: updating the DOM and drawing it.
React DevTools shows the rendering part. The browser Performance panel shows the whole picture. Starting with a measurement tells you which of these deserves attention.
Exercise
A teammate says a page feels slow and proposes wrapping every component in React.memo. Give two things you would do first, and one reason not to start with memo.
Show answer
First, ask which interaction is slow and record it to get a baseline number, then find which components render and why. Do not start with memo because it adds comparison work and code complexity, and it does nothing if the slowness comes from something other than re-rendering, such as a large bundle or a slow network request.