Serksa
All Concepts
Frontend Architecture

Code Splitting

1

What is it?

<strong>Code splitting</strong> is breaking your JavaScript bundle into smaller chunks that can be loaded on demand. Instead of one huge file, users download only the code they need for the current page.

2

Think of it like...

The Encyclopedia Analogy

Instead of carrying the entire encyclopedia, you take only the volume you need. Code splitting delivers only the JavaScript needed for the current page.

📚

Full Encyclopedia (Monolithic Bundle)

Heavy, all at once

📖

Individual Volumes (Code Chunks)

Load what you need

👤

Reader (User)

Gets relevant content

3

Visual Flow

📦Build

Split into Chunks

🌐Route

Load Route Chunk

Execute

Run Only Needed Code

4

Where you see it

1

Define split points

const Home = lazy(() => import('./Home'))

2

Build creates chunks

Webpack/Vite creates separate files

3

User visits route

Navigate to /dashboard

4

Load chunk on demand

Download dashboard.chunk.js

5

Execute and render

Run code and show page

5

Common Mistake

Wrong

"Code splitting is only for large apps"

Correct

<strong>Any app benefits from code splitting</strong>. Even small apps have routes users don't visit. Why make them download code for pages they never see?

💡 Real-World Example

E-commerce site:

1

Without splitting: 2MB bundle (Home + Products + Checkout + Admin)

2

With splitting: 200KB home chunk, load others on demand

3

User visits home → 200KB (10x smaller!)

4

User visits checkout → Load checkout chunk (300KB)

5

Admin users → Load admin chunk (500KB)