Skip to content
fa
Search Frontend Atlas
Search questions and concepts across the handbook.
Performance & Reliabilityintermediate10 min read

How do you reduce JavaScript cost without blindly splitting everything?

Analyze parse/compile/execute cost, dependency weight, route reachability, and interaction timing.

The JavaScript sent to a browser has several costs. It takes time to download, parse, compile and execute, and those costs are more noticeable on slower devices. A bundle budget gives a team a way to notice when a feature makes the initial experience heavier before the cost quietly accumulates.

The JavaScript sent to a browser has several costs. It takes time to download, parse, compile and execute, and those costs are more noticeable on slower devices. A bundle budget gives a team a way to notice when a feature makes the initial experience heavier before the cost quietly accumulates.

The quick version

Measure route-level JavaScript, identify high-cost dependencies and execution paths, then defer code that is not needed for the initial experience. Code splitting helps only when the deferred code is not immediately fetched and executed anyway.

Build the mental model

Dynamic imports create chunk boundaries, but too many tiny chunks can increase request and coordination overhead. Tree shaking requires static analyzability and correct side-effect metadata. Reduce unnecessary client hydration as well as bundle bytes.

See it in code

const Editor = lazy(() => import('./Editor.js'));

Where it gets tricky

Splitting code can improve the initial route while making a later interaction slower if its chunk is fetched only after the user clicks. Preload only when the probability and urgency justify it. Inspect duplicate versions of dependencies and expensive initialization, not just compressed byte size; parse and execution cost can dominate on slower CPUs.

A common misunderstanding

A smaller compressed bundle can still be expensive to parse and execute, and a large split bundle may still be downloaded eagerly.

What this changes in an application

Track initial and interaction-triggered JS separately. Test on constrained CPUs and realistic network profiles.

Questions worth exploring next

  • How do you find duplicate dependencies?
  • What is the trade-off between preloading and lazy loading?

References and next steps