Middle+
What do you mean by code splitting?
sobes.tech AI
Answer from AI
Code splitting is an optimization technique for loading web applications, where a large JavaScript bundle is divided into smaller parts (chunks). This allows loading only the necessary parts of the code during the initial page render, and the rest as needed, for example, when navigating to another page or calling a specific function.
Advantages of code splitting:
- Reduces initial load time
- Improves application performance and responsiveness
- Efficient use of browser cache
Example using dynamic import in React:
import React, { Suspense, lazy } from 'react';
const LazyComponent = lazy(() => import('./LazyComponent'));
function App() {
return (
<div>
<Suspense fallback={<div>Loading...</div>}>
<LazyComponent />
</Suspense>
</div>
);
}
Here, LazyComponent will be loaded only when it is actually needed, not together with the main code.