liveBook · Manning

Lazy initialization

As explained in Secrets of the JavaScript Ninja, Third Edition

Deferred loading is the postponement of loading a dependency until it is needed or otherwise appropriate.

Lazy loading postpones loading a dependency until it is needed or otherwise appropriate. This can keep code that never runs from consuming download bandwidth and parsing time. section 1.4.1

ES modules make dependencies explicit, allowing tools to identify unused code and defer loading dependencies. For code not needed on the initial render, `import()` can move modules into separate chunks. section 13.1 section 13.3.1

When deciding when code should load:

  • Keep code needed for the initial render available; lazy-load code that is not needed yet. section 13.3.1
  • Consider prefetching a lazy-loaded chunk so it is cached before the user triggers its import. section 13.3.1

A sign-up funnel

A site could split its landing and sign-up pages into separate chunks, then prefetch the sign-up chunk from the landing page. section 13.3.1

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Explains module-based deferral and chunking, but does not provide a worked `import()` implementation.
Examples
Uses code outside the initial render and a landing-to-sign-up funnel with a prefetched page chunk.
What you can do after
Apply lazy loading with `import()` and coordinate separate chunks with prefetching.
How it connects
Connects lazy loading to ECMAScript modules, dependency graphs, code splitting, tree shaking, and prefetching.

Also explained in

Compare how 12 books explain lazy initialization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Third Edition. The section links point to the parts of the book it draws on.