Bundle lab Β· ship less JavaScript

Remember the dist folder we dragged into Cloudflare in Session 1? That folder is the bundle β€” everything the browser has to download, parse and run. Here's how we keep it small.

A Β· Tree shaking

🌳 Ped hilao β€” sookhe patte gir jaate hain

Below is padhle-utils, a pretend utility library β€” but every byte counted is the real source code shown. Tick what your page actually uses, then shake the tree.

Whole libraryβ€”
What ships in your bundleβ€”

        

Real life: import _ from 'lodash' (CommonJS) ships the whole library. import debounce from 'lodash/debounce' or lodash-es ships one function and its helpers. Before adding any package, check its size on pkg-size.dev or bundlephobia.com.

B Β· Code splitting

🍿 Netflix poora season download nahi karta

Open DevTools β†’ Network β†’ JS first. Each button below downloads its code only when clicked. Watch a new file appear the first time β€” and not the second.

πŸ“Ί You're watching a lecture. The app shell is loaded. Nothing else has been downloaded yet.
App shell (this page)β€”
Downloaded on demand0 KB
Never downloadedβ€”
// chunk downloads appear here
// React
const TestAnalysis = lazy(() => import('./TestAnalysis'))
<Suspense fallback={<Skeleton />}><TestAnalysis /></Suspense>

// Next.js β€” plus: every page is already its own chunk
const TestAnalysis = dynamic(() => import('./TestAnalysis'), { loading: () => <Skeleton /> })

// Plain JavaScript
button.onclick = async () => { const { draw } = await import('./chart.js'); draw() }

These chunks are tiny (2–4 KB) so the demo stays light. In real apps the things worth splitting β€” a charting library, a PDF generator, a rich-text editor, a video player β€” are often hundreds of KB each.

C Β· The random letters in the filename

πŸ”‘ A fingerprint of the file's contents

The bundler names the output file after a hash of its contents:

A real one, from pw.live's HTML on 24 Sep 2026: webpack-899455d55a3e6270.js. Same code β†’ same name, forever. Changed code β†’ new name. Hold that thought β€” it is the whole secret of caching.