Skip to content

Lesson 8 of 9 · 28m

JavaScript Delivery: The Byte Is Only the Beginning

Reduce shipped JavaScript, split execution intelligently, understand hydration and long tasks, and optimize interaction responsiveness rather than bundle vanity metrics.

JavaScript is the only major web asset that keeps charging you after download. It parses, compiles, executes, allocates memory, hydrates DOM, wires events, and competes with user input on the main thread. Teams love to count kilobytes because kilobytes are visible in a pull request. Users experience long tasks, delayed input, jank, and battery drain. Optimize for the full cost.

Transfer size, parse cost, and execution cost are different

A 150KB compressed bundle can still be hundreds of kilobytes uncompressed and several hundred milliseconds of work on a mid-range phone. The simplified cost model is:

network transfer -> parse/compile -> execute -> framework hydration -> ongoing handlers

A desktop with a fast CPU can hide a shocking amount of waste. This is why performance complaints often come from users on devices your developers do not carry.

Long tasks explain INP better than bundle size

A long task is main-thread work over 50ms. Input handlers wait behind it. Timers, rendering, framework work, analytics, and third-party scripts all contribute:

const observer = new PerformanceObserver((list) => {
  for (const entry of list.getEntries()) {
    if (entry.duration > 100) {
      console.log("long task", entry.duration, entry.name, entry.startTime);
    }
  }
});
observer.observe({ type: "longtask", buffered: true });

Long tasks are not proof of one culprit. They tell you where responsiveness died; attribution requires traces, sourcemaps, and product knowledge.

Split by route and interaction, not by vibes

Route-level splitting is the baseline. Interaction-level splitting is often where real wins live:

const openSettings = async () => {
  const module = await import("./settings/SettingsPanel.js");
  module.mount(document.querySelector("#settings-root"));
};

Do not lazy-load code needed for first render. Do lazy-load code needed for rare workflows: charts, editors, export tools, payment flows, admin features, and heavy visualization. Prefetching can help, but only after critical resources have won.

Hydration is a tax on server-rendered HTML

A server-rendered page may look complete while remaining inert because JavaScript has not attached event handlers. Users tap a button, nothing happens, and INP or rage-click telemetry tells the story later. Islands, partial hydration, resumability, and progressive enhancement are different strategies for reducing this tax. The right answer depends on how interactive the page truly is.

A static marketing page does not need the same runtime as a dashboard. Shipping a full application framework to render mostly static content is a product architecture decision, not an inevitable consequence of modern development.

Third parties are unowned main-thread code

Tag managers turn your performance budget into a shared kitchen. One vendor adds a heatmap, another adds attribution, another adds a chat widget, and suddenly checkout input is delayed by code no engineer reviewed:

<script async src="https://cdn.vendor.example/widget.js"></script>

async prevents parser blocking but does nothing to limit execution cost after arrival. Audit vendor count, execution time, network impact, consent behavior, and business value. Remove scripts that nobody can justify. Delay nonessential widgets until interaction or idle time where product requirements allow.

Measure interaction, not just load

Synthetic trace examples should include actions: open the menu, type in search, add to cart, switch tabs, submit a form. A page can paint quickly and still feel broken during use. INP exists precisely because initial paint stopped being a sufficient proxy.

Field notes

  • Tree shaking helps only when dependencies are actually side-effect-free and import granularity allows dead code elimination.
  • Sourcemaps in production are useful for performance debugging; protect them appropriately instead of pretending obscurity is security.
  • Web workers help CPU-heavy work, but serialization and architecture complexity are real. Do not move trivial work into a worker because a benchmark looked dramatic.
  • A dependency that saves one day of development can cost every user time forever. Bundle review is a product review.

Was this lesson helpful?

Sign in to rate