Skip to content
IT glossary
FrontendUpdated December 3, 2026

Code splitting

Разделение кода

A build can package an app’s JavaScript into separate files so the browser loads the code for the current page first and fetches code for other pages or features later.

Also known as: lazy loading, ленивая загрузка

What code splitting does

Imagine a dashboard with a large spreadsheet library used only for exports. If the library ships in the startup bundle, everyone who opens the dashboard downloads it, even if they never export anything. Code splitting gives the export feature its own build output, called a chunk, so the dashboard can load without it. This can reduce both the bytes transferred at startup and the JavaScript the browser has to parse and execute. It does not necessarily reduce the total size of the build.

How it works

A bundler such as Webpack or Vite follows imports from the app’s entry points. A static import from startup code makes that dependency part of the startup work, even if the bundler puts it in a separate file. A dynamic import() gives the bundler a place to split: it can emit the requested module and its dependencies as one or more chunks. When the app calls import(), the browser fetches any files it still needs. The returned promise resolves to the module’s exports after evaluation, or rejects if loading or evaluation fails.

Example: Load a chart on demand

async function showChart(container) {
  container.textContent = 'Loading chart…';
  try {
    const { renderChart } = await import('./chart.js');
    container.textContent = '';
    renderChart(container);
  } catch {
    container.textContent = 'Could not display the chart.';
  }
}

Call showChart when the user opens the analytics tab. Assuming the chart code isn’t already required by the startup bundle or fetched ahead of time, opening the tab triggers the download. The loading message matters because the first visit to the tab now includes a network wait.

Where to split and what to watch for

Start with routes: visitors on the home page usually don’t need the settings screen’s code. Within a route, consider large features that only some users open, such as an editor or an export dialog. Avoid a separate chunk for every tiny component. Extra requests have overhead, and if one chunk discovers another only after it runs, those requests can form a slow chain. If users are very likely to open a feature next, fetching its chunk ahead of time can reduce that wait at the cost of extra network traffic.

Code splitting is about how code is packaged; lazy loading is about when it is fetched. A split chunk can be fetched eagerly, and lazy loading can apply to images as well as code. Tree shaking is different: it drops unused code instead of moving it into a later download.

In interviews

A common question is: “You added a dynamic import. Why is the initial JavaScript still just as large?” Another startup module might import the same library statically, or a large dependency might still be needed by the initial route. Use a bundle report to see what landed in each chunk, then confirm actual request timing in the browser’s Network panel. Compare cold-cache runs before and after the change, measuring both the initial JavaScript transfer and the delay when the deferred feature first opens.