Sub topic 3
Development and production builds: what your numbers mean
This chapter assumes you can build React function components with hooks, and that you have opened browser developer tools before. The same component can render at very different speeds depending on how you built and served the app. If you don't know which build you measured, you can't interpret a profiler number.
Why one app has two speeds
React ships in two flavors:
- Development build. This is what
npm run devserves. It includes extra checks, warnings, and bookkeeping to help you find bugs. - Production build. This is what
npm run buildemits. It is minified, and the checks and warnings are stripped out.
The development build does more work on every render. Its numbers are real, but they describe a slower program than the one your users run.
The main differences that affect timing are:
| Behavior | Development | Production |
|---|---|---|
| Warnings (keys, invalid props, deprecated APIs) | Checked on every render | Removed |
| `StrictMode` double rendering | Calls component bodies twice | Does nothing |
| `StrictMode` effect re-run on mount | Mounts, unmounts, and mounts again | Does nothing |
| Code size and shape | Unminified, with readable names | Minified |
| Hot reload and source-map overhead | Usually active | Absent |
| React DevTools Profiler | Works | Disabled unless you use a profiling build |
| `Profiler` component `onRender` | Fires | Does not fire unless you use a profiling build |
What changes in the numbers
Absolute durations are inflated
The development build validates props, tracks extra information about each element, and runs warning checks. This overhead is paid per component, so a deep tree pays it many times. How much slower development is depends on your tree. A common experience is a factor of two or more, and for heavy trees it can be much larger. Treat any specific multiplier as a guess until you measure it in your own app.
This means a component showing 40 ms in development might take 10 ms in production. It also means you may chase a "slow" component that is already fast for users.
Render counts are mostly stable, except under StrictMode
StrictMode is a component that checks your code for side effects in render. In development it calls your component functions twice per render and re-runs effects once on mount. The Profiler usually reports one commit, but your own console.log inside a component body will print twice.
If you count renders by logging, the count in development can be double the real one. Do not conclude that you have a re-render bug until you check this. The Profiler's "why did this render" information (covered in a later chapter) is more reliable than counting logs.
Relative comparisons usually hold, but not always
If component A takes five times longer than component B in development, it is probably the slower one in production too. The ranking is a good clue. The ratio is not guaranteed, because the extra checks cost more in some components than in others. Use development to find suspects. Use production-like builds to judge how bad they are.
Detecting which build you are running
Check before you record anything. Two quick signals help:
- The React DevTools extension icon in the browser toolbar. It is typically colored for production pages and has a different appearance for development pages. Hover over it to read the message, such as "This page is using the production build of React" or "This page is using the development build of React".
- A visible marker you add yourself. A marker is useful because it works in every environment.
This example uses Vite, which exposes import.meta.env.PROD as true in a production build:
export function BuildBadge() {
const label = import.meta.env.PROD ? "production build" : "development build";
return <small style={{ position: "fixed", bottom: 4, right: 4 }}>{label}</small>;
}If you use webpack or Create React App, check process.env.NODE_ENV === "production" instead. Remove the badge before you ship, or limit it to non-user-facing environments.
The profiling build
A normal production build removes the instrumentation that the Profiler needs. That leaves a gap. Development timings are inflated, and a plain production build can't be profiled.
React provides a third option, the profiling build. It is an optimized production build that keeps the profiling hooks. Its timings are close to what users see, and the DevTools Profiler and the Profiler component's onRender callback both work in it. The cost is a small amount of extra overhead, so don't ship it to users.
You enable it by aliasing react-dom to its profiling entry point at build time. In Vite, add a dedicated mode:
// vite.config.js
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
export default defineConfig(({ mode }) => ({
plugins: [react()],
resolve: {
alias:
mode === "profiling"
? { "react-dom/client": "react-dom/profiling" }
: {},
},
build: {
// Keep component names readable in the Profiler.
minify: mode === "profiling" ? false : "esbuild",
},
}));Build and serve it locally:
npx vite build --mode profiling
npx vite previewOpen the preview URL and record with the DevTools Profiler. You should see component names and real timings. Disabling minification keeps names readable, but it also changes the bundle, so numbers can drift slightly from the shipped build. If your minifier keeps function names, you can leave it on.
In webpack, the equivalent is an alias such as "react-dom$": "react-dom/profiling" in resolve.alias. Check the documentation for your React version, because entry points and the setup can change between major releases.
Making your measurements trustworthy
A production-like build is necessary but not sufficient. These habits remove other sources of noise:
- Use a clean browser profile. Extensions inject scripts and run work on the page. A private window with extensions disabled is a quick option. Keep React DevTools enabled for that window if you need the Profiler.
- Throttle the CPU. Your development machine is probably faster than your users' devices. In the browser's Performance panel, set CPU throttling to 4x or 6x slowdown. Short renders then become visible, and your relative comparisons get more reliable.
- Repeat each recording. The first interaction includes cold caches and JIT compilation. Warm up the page, then record the same interaction several times and compare.
- Close other heavy tabs and apps. Background work affects timings.
- Keep data the same. Use the same list size, the same user, and the same API responses for before and after.
The next diagram summarizes which build to use for each question.
Common mistakes
- Optimizing from development numbers alone. You add
memoeverywhere because dev showed 60 ms, and production would have shown 12 ms. The change adds code and comparison cost for a gain nobody notices. - Trusting double logs. Seeing every
console.logtwice and "fixing" renders that never happened twice in a real commit. - Seeing no data in production. Opening the Profiler on a plain production build and finding it empty or disabled. The cause is the missing profiling build, not a broken extension.
- Comparing across builds. Recording "before" in development and "after" in a profiling build. Any improvement you see is partly the build change. Always keep the build the same for both recordings.
- Forgetting throttling. A fast laptop can hide a problem that a mid-range phone will show clearly.
A practical workflow
- Reproduce the slow interaction in development. Use the Profiler's ranking to pick one or two suspects.
- Build with the profiling mode and serve it locally.
- Record the same interaction with CPU throttling enabled. Note the commit durations for your suspects.
- Decide whether the number is a problem. A commit that fits in a single 16 ms frame at 60 fps is rarely worth fixing. A commit taking 100 ms or more during typing is.
- Apply one fix, rebuild with the same mode, and repeat the recording.
The final chapter returns to this idea and turns it into a repeatable check.
Exercise
You profile a form in npm run dev and see one input component take 45 ms per keystroke, and its console.log prints twice per keystroke. A colleague says it renders twice and proposes wrapping it in React.memo. What should you do first, and why?
Show answer
First confirm which build you measured and whether StrictMode is on. In development, StrictMode calls component bodies twice, so the double log likely comes from that and not from two real renders. The 45 ms is also inflated by development checks. Build with a profiling build, enable CPU throttling, and record the keystroke again. Only if the production-like timing is still a problem, and the Profiler shows the component re-rendering without a prop or state change, should you consider React.memo or moving state down.
Exercise
You record a session on a plain production build, and the DevTools Profiler shows nothing useful. Which change enables profiling while keeping production-like performance, and which kind of environment should serve it?
Show answer
Build with the profiling build by aliasing react-dom/client (or react-dom, depending on your setup) to react-dom/profiling. The result is optimized like production but keeps the instrumentation the Profiler and the onRender callback need. Serve it only on your own machine or on a staging environment, not to real users, because it has a little more overhead than a plain production build.