What is Code Splitting? Dynamic Imports and Lazy Loading
Learn how code splitting loads JavaScript in smaller chunks on demand, with dynamic import() and React lazy loading examples.

What is Code Splitting?
As an app grows, its JavaScript can include code for many screens and features. If all of that code is loaded before the user can interact, the initial download may be larger than it needs to be.
Code splitting divides an application's code into separate bundles or chunks that can be loaded independently. The initial page can load the code it needs first; other chunks can be requested later when a feature or route is needed. Dynamic imports are a common way to mark code that can load on demand. A bundler or framework then decides how to produce and load the chunks. MDN: Code splitting · MDN: Lazy loading
Code splitting can reduce the JavaScript needed for the initial view, but it is not an automatic speed boost in every case. It may add a network request or a visible loading pause when the user opens that feature. Split around meaningful features that are not needed immediately.
Dynamic import in JavaScript
Static imports are loaded as part of the module's dependency graph. A dynamic import() returns a Promise and loads a module when execution reaches that expression:
const chartButton = document.querySelector("#show-chart");
chartButton.addEventListener("click", async () => {
try {
const { showChart } = await import("./chart.js");
showChart();
} catch (error) {
console.error("Could not load the chart feature:", error);
}
});
The chart.js module can export the feature:
export function showChart() {
console.log("Render the chart here");
}
The module is requested only after the button is clicked. When using a bundler such as Vite or another build tool, dynamic imports commonly become separate build chunks. The browser or bundler must be able to resolve the module path, and the generated chunks must be included in the deployment. MDN: Dynamic import()
Lazy-load a React component
In React, lazy() can load a component through dynamic import. Wrap the lazy component in a <Suspense> boundary so React can show a fallback while the component code loads:
import { lazy, Suspense, useState } from "react";
const ReportsPage = lazy(() => import("./ReportsPage.jsx"));
function App() {
const [showReports, setShowReports] = useState(false);
return (
<>
<button onClick={() => setShowReports(true)}>Open reports</button>
{showReports && (
<Suspense fallback={<p>Loading reports…</p>}>
<ReportsPage />
</Suspense>
)}
</>
);
}
ReportsPage.jsx should default-export the component in this example:
export default function ReportsPage() {
return <h1>Reports</h1>;
}
React calls the lazy loader when the component is first rendered and shows the nearest Suspense fallback while it waits. The loader should return a Promise whose resolved module has a default component export. Your bundler or framework must support this dynamic-import pattern. React: lazy · React: Suspense
Common places to split
- Routes or screens the user may not open during a visit.
- Large editors, charts, maps, or other feature-specific tools.
- Rarely used dialogs or admin-only parts of an app.
Keep essential first-screen code available immediately. Too many tiny chunks can add overhead and make loading behavior harder to follow. Measure the production build and user experience before and after changing the split points.
Try it yourself
Move a non-essential feature, such as a chart helper or report panel, into its own module. Load it with import() only after a button click. If you use React, try the lazy() and <Suspense> example and change the fallback message.
Recap
- Code splitting divides app code into independently loadable chunks.
- Dynamic
import()loads a module when execution reaches it and returns a Promise. - React
lazy()and<Suspense>provide a component-level lazy-loading pattern. - Splitting can reduce initial JavaScript, but later chunks still take time to load; choose useful boundaries and measure.
What's next?
Next, we’ll start the React series with React Series.
Written by
Dev Likhon
Full Stack Developer & Tech Educator
6+ years of professional development experience.
About Me
Comments
Share what you learned or ask a question.
Loading comments...