JavaScript Internals (521 Qs)•Part 3: DOM, Event Flow, Timers & Browser APIs (104 - 173)
What is the need of tree shaking
Tree Shaking can significantly reduce the code size in any application. i.e, The less code we send over the wire the more performant the application will be. For example, if we just want to create a “Hello World” Application using SPA frameworks then it will take around a few MBs, but by tree shaking it can bring down the size to just a few hundred KBs. Tree shaking is implemented in Rollup and Webpack bundlers.
Practical Code Example
javascript
// Need for Tree Shaking: Smaller JavaScript bundles lead to:
// 1. Faster download over mobile networks
// 2. Less CPU parse and compile time in browser V8 engine
// 3. Lower Interaction to Next Paint (INP) and faster Time to Interactive (TTI)