Sub topic 3
Development and production builds: what your numbers mean
This chapter assumes you can build React function components with state and hooks, and that you have opened browser developer tools before. You will learn why a profile from a development build can mislead you, and how to get numbers that match what users see.
Why the two builds differ
React ships two builds of its code. The development build adds extra work to help you find bugs. The production build removes that work.
The development build does these things that cost time:
- It checks props and hook usage and prints warnings.
- In Strict Mode, it renders each component twice to expose impure code. It also runs effects an extra time on mount.
- It keeps extra data for debugging tools.
The production build is minified, which means the code is shortened to reduce size. It also drops the checks above. Rendering is often several times faster.
A development profile therefore overstates render time. It can also change the shape of the problem. A component that looks slow in development may be fast in production, and the reverse is rare but possible.
What to trust in a development profile
Not every number is equally unreliable.
| Measurement | Development build | Production build |
|---|---|---|
| Absolute time in ms | Inflated, do not quote | Realistic |
| Which components rendered | Reliable, but Strict Mode doubles some renders | Reliable |
| Why a component rendered | Available | Needs a profiling build |
| Relative ranking of slow components | Usually a good hint | Reliable |
Use development for finding what renders and why. Use production for deciding how slow something is.
Profiling a production build
A plain production build has profiling turned off, so the DevTools Profiler tab has little to show. Most tools let you create a production build that keeps profiling support. In Vite, for example, you can alias react-dom to its profiling build:
// 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: {
minify: mode === "profiling" ? false : "esbuild",
},
}));Build and serve it with these commands:
npx vite build --mode profiling
npx vite previewTurning off minification here keeps component names readable in the Profiler. Your project may use a different bundler, so check its documentation for the equivalent setting. The react-dom/profiling entry point is the part that stays the same.
Do not ship this build. The profiling build is slightly slower than the normal production build.
Other things that skew results
- Browser extensions. Ad blockers and similar tools add work to every page. Profile in a clean browser profile or a private window.
- Fast hardware. Your laptop is faster than many users' phones. Use CPU throttling in the browser Performance panel to approximate slower devices.
- Single runs. Timings vary between runs. Record the same interaction three times and compare.
- Cold starts. The first render runs code the engine has not optimized yet. Separate "mount" results from later updates.
A repeatable routine
Write down your baseline number before changing code. The later chapter on verification relies on it.
Exercise
A teammate reports that a table component takes 48 ms to render in the Profiler while running npm run dev, and wants to rewrite it. What do you tell them to do first, and why?
Show answer
Rebuild the app as a production build with profiling support, and record the same interaction several times, ideally with CPU throttling. The development build runs extra checks and, in Strict Mode, renders twice, so 48 ms overstates the real cost. Only a production measurement shows whether the table is actually slow enough to need a rewrite.
Exercise
Which kinds of information from a development profile can you still rely on?
Show answer
Which components rendered, why they rendered, and roughly which ones are slowest relative to the others. Absolute millisecond values are the part to distrust.