Chapter 75: JavaScript, browsers and performance
JavaScript is the programming language browsers run to change a page after it has loaded. HTML gives a page its structure and CSS its appearance (→ 74 HTML and CSS); JavaScript opens the menu, checks the form and fetches the next page of results. It’s also the most common reason a well-designed page feels slow.
The DOM and events
The DOM (Document Object Model) is the tree of objects a browser builds from a page’s HTML. Each item in the tree is a node: an element such as a <button>, or a run of text inside it. MDN describes the DOM as a programming interface that lets scripts “change the document structure, style, and content.”1 When a script adds a product card or hides an error message, it changes the DOM, and the browser redraws the page to match.
The DOM isn’t part of JavaScript. It’s a web API the browser provides, and JavaScript is the language most often used with it. JavaScript running on a server, in Node.js, has no DOM at all.1
An event is a signal the browser fires when something happens: a click, a key press, a form being submitted, a page finishing loading. An event listener is code attached to an element that waits for one kind of event and runs a function, called the event handler, when it fires.2 A button with a listener for click does something when pressed; a button without one does nothing, however good it looks.
So interaction specs need to name the trigger. Hover, focus, click and key press are different events, and a menu that opens only on hover never opens for someone using a keyboard or a touchscreen (→ 81 Handoff and accessibility in code).
Frameworks and components
A framework is a library of code that sets how an interface is built and updated. Writing every DOM change by hand gets unmanageable on a large product, so most teams use one: React, Vue, Angular or Svelte.
Most frameworks are organized around the component, a reusable piece of interface. React’s documentation defines a component as “a JavaScript function that you can sprinkle with markup,”.3 A ProductCard component is written once and placed wherever a card is needed.
A component also holds state, the data it has at a given moment: whether a menu is open, or what’s typed in a field. When state changes, the framework redraws the parts of the component that depend on it. A designer’s list of states for a component (→ 61 Interaction patterns, forms and states) is, in code, a list of the values its state can take.
Components in a design file and in code are separate objects meant to match; keeping them in step is much of a design system’s job (→ 65 Design systems).
How a browser renders a page
Turning HTML into pixels takes a fixed sequence of steps, which MDN calls the critical rendering path:4
- The browser parses the HTML into the DOM, a bit at a time as the file arrives.
- It parses the CSS into the CSSOM, a matching tree of styles. This can’t be done a bit at a time.
- It combines the two into the render tree, which holds only what will be visible. Anything set to
display: noneis left out. - Layout works out the size and position of every element in the render tree.
- Paint fills in the pixels. Separately painted layers are then combined into the final frame.
CSS is render-blocking: the browser shows nothing until it has loaded and processed all the CSS, because a later rule could overwrite an earlier one.4 An ordinary <script> tag is worse, because HTML parsing stops while the script downloads and runs. Developers mark scripts defer or async so building can continue. Every web font, carousel and analytics tag a design asks for adds to this path.
Most of this work happens on the main thread, which also runs nearly all the page’s JavaScript, one task at a time. A long task is any task on the main thread that takes longer than 50 milliseconds. While one runs, the page can’t respond to a tap.5 The usual fix is to split work into smaller pieces so the browser can handle input in between.
Core Web Vitals
Core Web Vitals are three metrics Google publishes for loading, responsiveness and visual stability. The thresholds, from web.dev, are:6789
| Metric | Measures | Good | Poor |
|---|---|---|---|
| LCP | Loading | 2.5 s or less | Over 4.0 s |
| INP | Responsiveness | 200 ms or less | Over 500 ms |
| CLS | Visual stability | 0.1 or less | Over 0.25 |
Values in between need improvement. A page passes when it meets all three “good” thresholds at the 75th percentile of page loads, measured separately for mobile and desktop.6 That means field data, measured on real visitors’ devices and connections, not a test on the designer’s own laptop.
Largest Contentful Paint (LCP) is the time from navigation until the largest image, video or text block in the viewport is drawn.7 On most pages that’s the hero image or headline, so the design decides what LCP measures. A 3 MB hero photo is a design choice with a timing cost (→ 35 Resolution, formats and units).
Interaction to Next Paint (INP) is the delay between a click, tap or key press and the next frame the browser draws. It’s taken from the slowest interaction in a visit, ignoring outliers. Scrolling and hovering don’t count. Each interaction’s delay has three parts: waiting for the main thread, running the event handlers, and drawing the result.8 INP replaced First Input Delay as a Core Web Vital in March 2024; the older metric measured only the wait before the first interaction.10
Cumulative Layout Shift (CLS) scores how much visible content moves unexpectedly. Each shift is scored by the share of the viewport that moved multiplied by how far it moved, and CLS is the worst burst of shifts in a visit. Shifts within half a second of a tap or key press don’t count, because the user caused them. The common causes are images without set dimensions, ads and embeds that resize, web fonts sized differently from their fallbacks, and content inserted above what the reader is looking at.9 Most of these are fixed in the design by reserving space.
redBus, an Indian bus-ticketing site, reported that it fixed slow interactions on its search results by loading 10 results per request rather than 30, which brought INP on that page from about 870–900 ms to about 350–370 ms. It reported a 7% rise in sales after the work, though several changes shipped together and no controlled test isolated INP’s effect.11
Perceived performance
Perceived performance is how fast a site seems to the people using it. MDN calls it harder to measure than real speed and possibly more important. Its recommendations are mostly design decisions: show content as it arrives, load what people will use first, reserve space so nothing jumps, and pick fallback fonts close in size to the web font.12
The way a wait is shown changes how long it feels. In a 2007 study at Carnegie Mellon and AT&T Labs, 22 participants compared progress bars of identical length. Bars that sped up toward the end seemed faster than a steady bar, and bars that paused near the end seemed slower.13 In a 2010 follow-up, a bar with animated ribbing moving backwards against the direction of progress cut perceived duration by 11%.14 The first study was small and the second used short bars, so treat the findings as direction, not exact figures.
Which loading pattern to use for which wait (skeleton screen, spinner or progress bar) is covered in → 61 Interaction patterns, forms and states, and the response-time limits behind them in → 46 Affordances, signifiers, feedback and constraints.
See also
73 How the internet and the web work · 74 HTML and CSS · 76 Frontend, backend and architecture · 61 Interaction patterns, forms and states · 65 Design systems · 81 Handoff and accessibility in code
Sources
- Harrison, C., Amento, B., Kuznetsov, S. and Bell, R. (2007). “Rethinking the Progress Bar.” Proceedings of UIST ‘07. ACM.
- Harrison, C., Yeo, Z. and Hudson, S. E. (2010). “Faster Progress Bars: Manipulating Perceived Duration with Visual Augmentations.” Proceedings of CHI 2010. ACM.
- Kumar, A. and Rajpal, S. (2023). “How redBus improved their website’s Interaction to Next Paint (INP) and increased sales by 7%.” web.dev.
- MDN Web Docs (n.d.). “Introduction to the DOM”; “Introduction to events”; “Critical rendering path”; “Perceived performance.” Mozilla.
- React (n.d.). “Your First Component.” react.dev.
- Viscomi, R. (2024). “Interaction to Next Paint is now a stable Core Web Vital metric, replacing First Input Delay.” web.dev.
- web.dev (2019–2025). “Web Vitals”; “Largest Contentful Paint (LCP)”; “Interaction to Next Paint (INP)”; “Cumulative Layout Shift (CLS)”; “Optimize long tasks.” Google.
Notes
- MDN Web Docs (n.d.). “Introduction to the DOM.” Mozilla. https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction ↩ ↩ ↩
- MDN Web Docs (n.d.). “Introduction to events.” Mozilla. https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Scripting/Events ↩
- React (n.d.). “Your First Component.” react.dev. https://react.dev/learn/your-first-component ↩
- MDN Web Docs (n.d.). “Critical rendering path.” Mozilla. https://developer.mozilla.org/en-US/docs/Web/Performance/Guides/Critical_rendering_path ↩ ↩
- Wagner, J. and Kenny, B. (n.d.). “Optimize long tasks.” web.dev. https://web.dev/articles/optimize-long-tasks ↩
- Walton, P. (2020, updated 2024-10-31). “Web Vitals.” web.dev. https://web.dev/articles/vitals ↩ ↩
- Walton, P. and Pollard, B. (2019, updated 2025-09-04). “Largest Contentful Paint (LCP).” web.dev. https://web.dev/articles/lcp ↩ ↩
- Wagner, J. and Pollard, B. (2022, updated 2025-09-02). “Interaction to Next Paint (INP).” web.dev. https://web.dev/articles/inp ↩ ↩
- Walton, P. and Mihajlija, M. (n.d.). “Cumulative Layout Shift (CLS).” web.dev. https://web.dev/articles/cls ↩ ↩
- Viscomi, R. (2024, last updated 2024-03-12). “Interaction to Next Paint is now a stable Core Web Vital metric, replacing First Input Delay.” web.dev blog. https://web.dev/blog/inp-cwv-launch ↩
- Kumar, A. and Rajpal, S. (2023, last updated 2023-05-10). “How redBus improved their website’s Interaction to Next Paint (INP) and increased sales by 7%.” web.dev case study. https://web.dev/case-studies/redbus-inp ↩
- MDN Web Docs (n.d.). “Perceived performance.” Mozilla. https://developer.mozilla.org/en-US/docs/Learn_web_development/Extensions/Performance/Perceived_performance ↩
- Harrison, C., Amento, B., Kuznetsov, S. and Bell, R. (2007). “Rethinking the Progress Bar.” UIST ‘07, 115–118. https://www.chrisharrison.net/index.php/Research/ProgressBars ↩
- Harrison, C., Yeo, Z. and Hudson, S. E. (2010). “Faster Progress Bars: Manipulating Perceived Duration with Visual Augmentations.” CHI 2010, 1545–1548. https://www.chrisharrison.net/index.php/Research/ProgressBars2 ↩