Code splitting and lazy loading are techniques used to improve performance by loading only the code that is needed, when it is needed. This reduces the initial bundle size and speeds up page load time.
Code splitting breaks your application into smaller JavaScript bundles instead of loading everything at once.
Benefits:
Faster initial load
Better performance on slow networks
Improved user experience
Lazy loading loads components only when they are rendered, rather than at app startup.
React provides React.lazy() and Suspense for lazy loading components.
React.lazy() dynamically imports a component
Suspense shows fallback UI while loading
Lazy loading can fail (e.g., network issues). Use error boundaries.
For non-components:
Lazy load routes and large components
Do not lazy load small, frequently used components
Use meaningful fallback UI
Combine with code splitting by routes
Take quizzes related to this topic and see where you stand!
Start Quiz Now