HTML Performance and Rendering Cost

Intermediate · 29 min

How the markup you ship affects load and rendering: blocking scripts, lazy images, reserved image space, DOM size, content-visibility, and LCP's four phases.

Before thisHTML Validation and DebuggingScript Loading, Async, Defer and ModulesResource Hints, Preload, Prefetch and PreconnectLazy Loading and Image Decoding

Chrome for Developers · 29 min

HTML Performance and Rendering Cost

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Chrome for DevelopersReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Performance is mostly decided by what the HTML asks the browser to fetch, when, and how much work the result costs to lay out. The markup is the first lever.
  • A blocking script in the head ran before the page's body script, and `defer` ran scripts in order after parsing and before `DOMContentLoaded`. `async` ran in arrival order, with the faster script first, in a test.
  • `loading="lazy"` skipped images far below the fold: 12 of 30 stacked images were requested instead of 30 in Chromium.
  • Images without `width` and `height` caused a layout shift when they loaded (CLS 0.03 in a test). With the attributes, CLS was 0.
  • Big DOMs cost on every style and layout pass. A style recalculation plus layout took about 4 ms with 1,000 elements and about 119 ms with 100,000.

Two costs: getting bytes, and doing work with them

A page can be slow because it waits (for HTML, scripts, images) or because the browser works hard once it has them (style, layout, paint). HTML influences both, long before CSS or JavaScript get clever. How a browser turns markup into pixels is in the frontend lesson on how browsers render a page; here is the HTML-author’s slice.

All measurements below are Chromium 159, run locally with fake delays, so read them as direction, not benchmarks.

Scripts decide when parsing waits

A normal <script src> in the head blocks parsing. In the test, with two such scripts, both ran while document.readyState was loading, before the inline script further down the body. With defer, the inline script ran first and the deferred scripts ran afterwards, in document order (the slow one first even though the fast one arrived sooner), at interactive, before DOMContentLoaded. With async, DOMContentLoaded came first and the scripts ran in arrival order, fast one first. Use defer for scripts that need the DOM or each other; the script loading lesson goes deeper, and resource hints cover fetching early.

Images: lazy, and sized

Thirty 300px images stacked in a 600px-tall viewport: all 30 were requested normally, and 12 with loading="lazy". The rest waited until scrolling approached. See lazy loading and decoding. The exception is the hero image: it is usually the LCP element and shouldn’t be lazy. fetchpriority="high" can raise its priority, and web-features lists fetchpriority as Baseline from 29 October 2024.

Space reservation matters as much as timing. A 600x400 image delayed by 400 ms, with max-width: 100%; height: auto, shifted the text below it when it arrived and recorded a layout shift of 0.03. With width="600" height="400" on the tag, the shift was 0. That is how the attribute pair works: the browser derives an aspect ratio from them before the file is known.

DOM size is a recurring bill

Every style or layout pass visits the tree. I added a stylesheet to pages of different size and forced layout: about 3.8 ms for 1,000 elements, about 119 ms for 100,000, and touching every wrapper and re-laying out took about 1 ms versus 80 ms. Nothing here is a hard limit, but deep, wide DOMs make every interaction pay. Prefer fewer wrapper elements, paginate long lists, and avoid rendering hidden content you don’t need.

Skipping work is another route. content-visibility: auto on 5,000 stacked sections cut an initial layout from about 56 ms to about 5 ms in the test, with contain-intrinsic-size set. web-features marks it Baseline from 15 September 2025. See CSS performance for the CSS side.

LCP in four parts

Largest Contentful Paint is the time until the biggest visible element renders. The usual breakdown has four phases: time to first byte, how long the browser takes to discover the resource (this is where the HTML matters: an image only referenced from CSS or JS is found late), how long the download takes, and how long the render waits after that. Frontend’s web performance lesson gives the wider metric set. Measure before optimising: DevTools’ Performance panel and Lighthouse show which phase dominates.

What the video covers

Philip Walton’s “A deep dive into optimizing LCP” from Google I/O 2022 (about 29 minutes). Its description says LCP is the hardest Core Web Vital for most sites to pass, and the talk breaks the metric down into a few simple parts to make it easier to reason about and improve. It is narrower than this lesson: it is about LCP, not DOM size or script timing, and it is from 2022. I could not read a transcript, so I describe the phases from the description and general knowledge. The measurements above are mine. This is one of the weaker fits in the group.

Where this fits

This follows validation and debugging and builds on the head and loading lessons. HTML email shows what happens when performance and features are constrained the other way.

Check yourself

  1. 01Why put `width` and `height` attributes on an `<img>` when CSS will resize it anyway?

    They let the browser compute an aspect ratio and reserve space before the file arrives. In a test the same delayed image caused a CLS of 0.03 without them and 0 with them.

  2. 02You have a blocking `<script src>` in the head. What does `defer` change?

    The script downloads in parallel and runs after parsing, in document order, before `DOMContentLoaded`. In a test a slow first script still ran before the quick second one under `defer`, and both ran after the body's inline script.

  3. 03Which image should not be `loading="lazy"`?

    The one that will be the page's largest visible element at load, usually the hero. Lazy-loading it delays the thing LCP measures. In a test, lazy loading helped only for images far below the viewport.

  4. 04Why might `content-visibility: auto` speed up a long page?

    Off-screen sections skip their rendering work until they get close. In a test, initial layout of 5,000 sections took about 56 ms with `visible` and about 5 ms with `auto`. Provide `contain-intrinsic-size` so scrollbars stay stable.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark