intermediate

Tree shaking and dynamic imports

Dead-code elimination, side effects, code splitting, lazy loading, and import timing.

Tree shaking removes unused exports when the module graph is statically analyzable and side effects are declared (`"sideEffects": false` in package.json).

`import('./chunk.js')` loads code asynchronously — used for route-level code splitting. Callers need loading and error UI states.

Too many small chunks can hurt performance through extra network round trips.

On interviews, explain the concept with a concrete example and name the runtime behavior interviewers probe.

Common pitfalls include mixing similar APIs and forgetting edge cases during live coding.

The trade-off is often clarity versus performance or safety versus convenience.

Checklist:

  • Use ESM for static analysis.
  • Mark side effects accurately in packages.
  • Handle loading/error for dynamic import.