Code splitting separates JavaScript that is essential for page startup from code needed only later, deferring the latter’s download and execution until needed. This can reduce the initial load and contention for bandwidth and CPU resources, potentially speeding up loading and improving responsiveness to input during startup. But splitting code into ever-smaller pieces is not always better: how code is split also affects request counts, compression efficiency, cache reuse, and script evaluation time.
Defer nonessential features, not all code
The benefit comes from reducing the work that must happen at startup. If the main thread is still busy processing JavaScript when a user begins interacting, their input may have to wait. Moving nonessential code out of the initial load can reduce the chance of that work overlapping with interactions. Code essential to the page’s operation must remain available; making the initial files smaller should not be the only goal.
Dynamic import() loads modules asynchronously and can be triggered by an interaction or other conditions. In web.dev’s example, the validation module is downloaded, parsed, and executed only when a form input field loses focus. Another scenario is loading a polyfill only when a particular browser needs it. These approaches shift work to the point when it is needed; they do not eliminate download and execution costs.
Code splitting still needs bundling; it does not mean sending raw modules individually
Dividing code into many modules during development helps with maintenance. However, sending a large, unbundled module tree directly to the browser requires a separate HTTP request for each module, which may delay the application becoming interactive. According to web.dev, bundling is still recommended for loading performance, even when modulepreload is used to load modules early.
Bundling and splitting are compatible: bundlers can process modules intended for deferred loading while balancing file size and compression, avoiding bundles so large that their evaluation becomes a long task. React.lazy is not a separate, independent splitting mechanism either; it still relies on dynamic import() under the hood, with the bundler producing separate chunks.
Smaller files offer caching benefits but come with compression and loading costs
- Cache reuse: If a framework and application are placed in a single large bundle, updating only the framework may invalidate the cache for the entire bundle. Splitting it into smaller files gives returning visitors a better chance of reusing unchanged resources.
- Compression and networking: Smaller files generally do not compress as efficiently as larger ones. When resources are not yet cached in the browser, they may also add network round-trip time.
- Main-thread workload: Splitting large scripts into smaller pieces can sometimes divide CPU-intensive evaluation work into shorter tasks, reducing the chance of prolonged blocking, but this is not guaranteed.
Choosing split points therefore requires considering both first-time and returning visitors. You can start by using Lighthouse to examine each script’s execution time, then use the Coverage tool in Chrome DevTools to identify code unused during loading as candidates for splitting. Code that is unused at that moment may still be needed later, so decisions must also account for the conditions that trigger each feature.
Code splitting: perspectives from across the web
What is the difference between async, initial, and all in webpack?
In web.dev's webpack example, async covers dynamically imported chunks, initial covers initial chunks such as those involving static imports, and all covers both and allows shared chunks. The default in the example is async. Changing it to initial places the form validation code in the same file as the main application code, potentially worsening initial loading performance. The option names alone are not enough to determine the outcome.
What happens if shared-chunk splitting is disabled?
In web.dev's webpack example, disabling SplitChunksPlugin by setting splitChunks: false causes the shared my-function.js module to be bundled into both main.js and form-validation.js, duplicating code. This is the outcome in that example, not evidence that every project will duplicate code in the same way. When reviewing the configuration, check which output files actually contain the shared modules.
Does using dynamic import() always trigger automatic code splitting?
Not necessarily; it depends on the bundler and its configuration. Most tools listed by web.dev automatically split code when they encounter dynamic import(), but esbuild requires this optimization to be explicitly enabled. Dynamic import() itself is syntax for loading modules asynchronously. To generate separate bundle chunks, you still need to check your tool's splitting settings rather than assume splitting has happened simply because import() appears in the code.
References
- Welcome to Learn Performance!web.dev — Source used in the article above.
- Code-split JavaScriptweb.dev — Discusses: What is the difference between async, initial, and all in webpack?; What happens if shared-chunk splitting is disabled?



