What is code splitting?
Code splitting is a technique for splitting the final bundle into several smaller parts instead of one large file. In Webpack this allows loading only the code that is actually needed at that moment.
Why it is used
- speeds up the initial page load - no need to load all the JS at once
- reduces the size of the main bundle
- loads additional modules "on demand"
Typical examples
- Splitting vendor code (React, lodash, moment, etc.) into a separate bundle
- Lazy loading pages in an SPA (dynamic
import()):
js
import('./Page.js').then(...)- Optimization via
splitChunksin Webpack
Summary
Code splitting makes the application faster and more efficient, because the user loads only the code they need, while the rest is pulled in later, as needed.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.