# Code Splitting

Code splitting divides an application into separately loadable chunks so a browser need not download and execute all code at startup.

- Type: practice
- Domain: Engineering & architecture
- Level: Intermediate
- Last reviewed: 2026-09-23
- Original: [Soroush’s Product Wiki](<https://wiki.soroush.uk/topic/code-splitting/>)

## What it means

Code splitting creates multiple JavaScript or CSS outputs around entry points, routes, components, or dynamic imports. The browser loads an initial set and retrieves other chunks when navigation or interaction requires them. It changes delivery boundaries; it does not by itself remove unused code or guarantee smaller total transfer.

## Why it matters

Reducing initial code can shorten download, parsing, and execution on constrained devices. Poor boundaries, however, create request waterfalls, duplicate dependencies, and delays at the moment a user needs a feature.

## When to use it

Use code splitting when measured bundles contain substantial code that many users do not need initially. Split around meaningful product paths, then preload or prefetch only likely next work rather than maximizing the number of chunks.

## How it works

1. Measure initial bundles and identify code not required for the first useful view.
2. Choose stable route, feature, or component boundaries for separate chunks.
3. Configure dynamic imports or build entry points and shared dependency handling.
4. Load chunks on demand with explicit loading, error, and retry states.
5. Monitor initial cost, cache reuse, request waterfalls, and interaction delay after deployment.

## A practical example

A commerce site moves its rarely used 3D product viewer into a separate chunk. Product pages become interactive sooner, and the viewer chunk starts only when a user opens the feature, with a retry state for network failure.

## Where it breaks

Splitting adds build and runtime coordination, chunk requests can fail, and too many small files increase overhead. Users who need every feature may download the same total code plus loader metadata.

## Common mistakes

- Creating tiny chunks without accounting for request and coordination overhead.
- Splitting critical interaction code that users need immediately.
- Ignoring duplicate libraries across chunks and cache invalidation behavior.
- Reporting smaller files without measuring execution and user-visible timing.

## Visualization

[View the visualization on the website](<https://wiki.soroush.uk/topic/code-splitting/#visualization-code-splitting>)

## Sources

1. [Lazy loading](<https://developer.mozilla.org/en-US/docs/Web/Performance/Guides/Lazy_loading>) — MDN Web Docs · Official technical documentation
2. [Code Splitting](<https://webpack.js.org/guides/code-splitting/>) — webpack · Official tool documentation

## Related topics

- [JavaScript Bundle](<https://wiki.soroush.uk/topic/javascript-bundle/>)
- [Lazy Loading](<https://wiki.soroush.uk/topic/lazy-loading/>)
- [Client-Side Rendering \(CSR\)](<https://wiki.soroush.uk/topic/client-side-rendering/>)
