Sub topic 14
Reducing load cost with code splitting
This chapter assumes you can build React function components with hooks, use dynamic import(), and read the Network and Performance panels in browser developer tools. The previous chapter showed how to find non-React bottlenecks. Here you fix one common one: a bundle that is too large.
Why bundle size is a load cost
The React Profiler measures rendering. It does not measure the time the browser spends downloading, parsing, and running your JavaScript before the first render. A single large bundle delays that first render, even if every component renders quickly.
You can usually spot this in the browser Performance panel as a long task at startup, labeled "Evaluate Script", and in the Network panel as one large JavaScript file. Code splitting divides the bundle into chunks. The browser loads the first chunk immediately and fetches the others when they are needed.
Splitting with React.lazy and Suspense
React.lazy takes a function that calls dynamic import(). Your bundler (Vite, webpack, and similar tools) sees that call and puts the imported module in its own chunk. Suspense shows a fallback while the chunk loads.
import { lazy, Suspense } from "react";
const Chart = lazy(() => import("./Chart"));
export function Report() {
return (
<Suspense fallback={<p>Loading chart...</p>}>
<Chart />
</Suspense>
);
}The Chart module is not downloaded until Report first renders it. ./Chart must use a default export, because lazy expects the module's default.
Where to split
Good split points are code the user may never reach, or will not need on first paint:
- Routes. Each page becomes its own chunk, so the home page does not carry the settings page.
- Heavy components such as charts, rich text editors, and maps.
- Rarely opened UI such as modals and admin panels.
Do not split small components. Each chunk costs an extra network request, and a tiny chunk can cost more in latency than it saves in bytes.
import { lazy, Suspense, useState } from "react";
const Editor = lazy(() => import("./Editor"));
export function Notes() {
const [editing, setEditing] = useState(false);
return (
<>
<button onClick={() => setEditing(true)}>Edit</button>
{editing && (
<Suspense fallback={<p>Loading editor...</p>}>
<Editor />
</Suspense>
)}
</>
);
}Here the editor chunk is fetched only after the user clicks Edit.
Common errors
- Named exports.
lazy(() => import("./Chart"))fails ifChartis a named export. Re-export it as default, or writeimport("./Chart").then(m => ({ default: m.Chart })). - No Suspense boundary. A lazy component rendered outside any
Suspensethrows an error. - Layout shift. A small fallback replaced by a tall component moves content on the page. Give the fallback a similar size.
- Splitting the critical path. Lazy-loading something visible on first paint adds a second round trip before the user sees it.
Verifying the change
Treat code splitting like any other optimization. Record the main bundle size and the startup long task in the Performance panel before the change. Make the change, then measure again in a production build. Development builds are larger and slower, so their numbers are not comparable.
Exercise
A dashboard's initial JavaScript bundle is 900 KB, and 400 KB of it is a charting library used only on a "Reports" tab. How would you reduce first-load cost, and what would you check afterward?
Show answer
Move the Reports tab's chart component behind React.lazy with a dynamic import(), and wrap it in a Suspense boundary with a fallback of similar size. Build for production, then confirm in the Network panel that the chart chunk loads only when the tab opens, and that the main bundle is smaller (around 500 KB). Re-record the startup in the Performance panel to see whether the script evaluation long task got shorter.