Sub topic 1
Why split code: bundles and load cost
Prerequisites
This chapter assumes you can write React function components and pass props. It also assumes you know useState and useEffect, ES module import and export, and the basics of promises. You should have used a bundler such as Vite or webpack at least once. Later chapters build on this one, so no React.lazy knowledge is needed yet.
What a bundle is
A bundler starts at your entry file and follows every static import. It merges everything it finds into one or more JavaScript files, called bundles. It also minifies them, which means it removes whitespace and shortens names.
By default, a static import always ends up in the initial bundle. This is true even if the component is only shown after a click or on a rarely visited page. The browser must download, parse, and run all of that code before the app becomes interactive.
Why bundle size costs time
Large bundles hurt in three separate ways:
- Network transfer. Bytes must cross the network. On a slow mobile connection, each extra 100 KB can add noticeable delay.
- Parse and compile. The browser's JavaScript engine must read the code and turn it into something it can run.
- Execution. Top-level code in every module runs at startup, including modules for screens the user never opens.
Items 2 and 3 use the CPU, so they cost more on low-end phones than on a developer laptop. Compression such as gzip or Brotli reduces transfer size only. It does not reduce parse or execution work.
What code splitting does
Code splitting means dividing your build output into several files, called chunks. The browser downloads only the chunks the current screen needs. It fetches the others later, when the user needs them.
The idea is simple. Code that is not needed to show the first screen should not delay the first screen.
Good candidates for splitting
Not every module is worth moving to its own chunk. Look for code that is large, rarely used, or both:
| Candidate | Why it is a good fit |
|---|---|
| Route-level pages | The user sees one page at a time. |
| Charting or editor libraries | They are often hundreds of KB and used on one screen. |
| Modals and drawers | They appear only after a user action. |
| Admin or settings screens | Most visitors never open them. |
| Heavy, rarely used features | Examples are PDF export or a rich-text editor. |
Small components usually should not be split. Each chunk needs its own network request, and the overhead of that request can exceed the savings.
A worked example
Suppose a dashboard imports a charting library statically:
import { useState } from 'react';
import Chart from './Chart'; // pulls in a large charting library
export default function Dashboard() {
const [showChart, setShowChart] = useState(false);
return (
<section>
<h1>Dashboard</h1>
<button onClick={() => setShowChart(true)}>Show chart</button>
{showChart && <Chart />}
</section>
);
}The chart is hidden until the button is clicked. Even so, the static import Chart places the chart code and the whole library in the main bundle. Every visitor pays for it, including those who never click.
Say the main bundle is 90 KB without the chart and 340 KB with it. Splitting the chart out lets the first screen load 90 KB. The remaining 250 KB downloads only when a user asks for it. These numbers are illustrative. Your own build output will tell you the real sizes.
You can see your sizes by building the project. With Vite, run this command:
npx vite buildThe output lists each emitted file with its size and gzipped size. Large entries in that list are your first candidates.
The trade-off
Splitting moves cost from startup to later. When the user opens the chart, the browser must fetch the chunk, so the user may wait. You then need a loading state and a plan for network failures. The next chapters cover the tools React provides for this: dynamic import(), React.lazy, Suspense, and error boundaries.
Exercises
Exercise
A page imports a rarely used PDF export module with a static import at the top of the file. The export button is only clicked by about 2% of visitors. Why does this still slow down the page for the other 98%, and what does code splitting change?
Show answer
A static import puts the module in the initial bundle, so every visitor downloads, parses, and runs it at startup. Code splitting moves the module into a separate chunk that is fetched only when needed, so the 98% who never click the button never pay for it.
Exercise
Why does gzip compression not fully solve the cost of a large bundle?
Show answer
Compression reduces only the number of bytes sent over the network. The browser still has to decompress, parse, compile, and execute all of the code, which costs CPU time, especially on low-end devices.