liveBook · Manning

Code splitting

As explained in Fast by Default

A software delivery technique that divides program code into separately loadable chunks so that only necessary code is initially delivered.

Code splitting divides an application into separately loadable code bundles so the initial download contains only the code needed to start the current experience. Instead of shipping every route, feature, stylesheet, or language resource up front, the application loads additional bundles when a user navigates to or opens the feature that needs them. The aim is to reduce download, parsing, and compilation work at startup without removing features. section 5.2.3 section 6.2.2

The technique addresses a recurring startup failure pattern: a product accumulates dependencies and feature code until every user pays for capabilities that only some users use. A user who opens one area of an application should not necessarily download code for unrelated routes, optional tools, or every supported language. Loading non-essential code only when needed keeps the first interaction lighter while preserving access to the full application later. section 5.2.3 section 6.1

Apply code splitting as an operational performance practice:

  • Set an explicit compressed JavaScript budget for the initial bundle, then reserve feature code for later loading where possible. section 6.2.2
  • Identify code that is not required for the first screen or core startup path, such as secondary routes, comments, video playback, photo filters, messaging, or compose-only controls. section 5.2.3 section 6.2.2
  • Make each proposed dependency visible against the budget. If it exceeds available headroom, choose a smaller alternative, load it only when needed, remove something else, or explicitly expand the budget. section 6.1 section 6.2.2
  • Measure initial payload separately from later bundles, and consider request-count budgets alongside byte budgets because many small requests can add delay on slow mobile connections. section 6.2.2

Feature bundles in a social application

A social application can keep its compressed main bundle to 180KB for the framework, routing, and core interface. The video player, photo filters, and messaging then become separate bundles loaded only when users request those features. A 45KB emoji-picker library may be acceptable when loaded from the compose screen, but putting it in the main download makes every visitor pay its cost whether or not they ever compose a post. section 6.2.2

Routes and translations on demand

An application with many routes and supported languages can avoid sending all of them at launch. Loading routes, stylesheets, and translations as navigation requires them prevents a user who needs one language from downloading the resources for all other languages. This approach differs from merely deferring execution: the unused code is not loaded initially at all. section 5.2.3

Table 6.1 Russell’s performance budget baselines
Page type
Example
JavaScript share
Total budget (3s load)
JavaScript-heavy
SPAs, client-rendered apps
50%
1.2MB
JavaScript-light
Server-rendered sites
15%
2.0MB

Use the baseline table to relate JavaScript weight to overall page budgets: JavaScript-heavy client-rendered applications allocate a larger share than JavaScript-light server-rendered sites. section 6.2.2

How Fast by Default covers it

How deep it goes
Explains bundle allocation through startup and feature examples, then ties decisions to compressed-byte and request-count budgets.
Examples
Uses on-demand routes and translations, article comments, social-media features, and a compose-only emoji picker.
What you can do after
Apply it through bundle budgets and conditional loading decisions for routes, libraries, and feature-specific code.
How it connects
Connects code splitting with JavaScript bundle budgets, dependency selection, request counts, routing, and startup performance.

Also explained in

Compare how 4 books explain code splitting →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fast by Default. The section links point to the parts of the book it draws on.