Shrink the DOM where it slows down style and layout work

Avoid an excessive DOM size was the Lighthouse audit that flagged a page once its body held more than about 800 elements. Lighthouse 13 replaced it with the Optimize DOM size insight, which still reports the element count, the deepest nesting and the element with the most children, but fails only when one style or layout update covering many elements takes longer than 40 ms. A large DOM hurts when changes to it make the browser recompute styles and positions for a lot of elements, so that's what the insight looks for.

What the Optimize DOM size insight reports

  • Total elements

    Every element on the page, including those inside shadow roots. Text and comment nodes aren't counted, and neither is anything inside an iframe.

  • DOM depth

    The deepest level of nesting. Depth alone doesn't slow a page, but deep nesting usually means wrapper elements the layout could do without.

  • Most children

    The element with the most direct children, often a long list, a big table or a menu rendered all at once.

  • Why it fails

    A layout touching more than 100 layout objects, or a style recalculation affecting more than 300 elements, that took longer than 40 ms. In the Performance panel the insight highlights those events in the flame chart.

Chrome's Optimize DOM size docs list these values and the failure rule. The old audit's limit comes from its final source code, in Lighthouse 12.8.2, which passed a page up to 818 body elements. Its guide also gives 1,400 nodes as a failure point, which that code didn't use as a separate limit. The replacement is listed in the Lighthouse 13 release notes.

Count the elements on your page

// Paste into the DevTools console after the page has loaded
// Skips elements inside shadow roots, which the insight counts
document.querySelectorAll("*").length;

// Find the element with the most direct children
[...document.querySelectorAll("*")].sort(
  (a, b) => b.childElementCount - a.childElementCount,
)[0];

Where large DOMs come from

  • Large DOMs usually come from rendering everything up front: every product in a long category, every row of a table, mega menus with hundreds of links, or separate phone and desktop versions of the same section with one hidden by CSS. Elements hidden with CSS still sit in the DOM and still count.

  • Component frameworks add depth when every component wraps its output in another element. web.dev's DOM size guide suggests fragments, which React, Preact, Vue and Svelte all offer, so a component can return several elements without a wrapper, and flexbox or grid to keep the layout working with flatter markup.

How to reduce DOM size

  1. Find the slow updateRecord a load and a few interactions in the DevTools Performance panel. Select a long Recalculate Style event, and its details show how many elements it affected.
  2. Render long lists in piecesShow the first part of a long list or table and load more on request, or use a windowing library that keeps only the visible rows in the DOM.
  3. Build hidden content when it opensLeave tabs, accordions, dialogs and menus out of the first HTML and create them the first time someone opens them.
  4. Skip rendering sections off screenGive large sections below the first screen content-visibility set to auto, so the browser skips their rendering work until they come near the viewport.
  5. Flatten the wrappersRemove wrapper elements the layout doesn't need, using fragments in your framework and flexbox or grid in your CSS.

Frequently asked questions

How many DOM elements is too many?

Lighthouse no longer sets a number. The old audit flagged pages with more than about 800 elements in the body, but the Optimize DOM size insight fails only on slow style or layout updates, so what matters is how much work your page's changes trigger across those elements.

Does a large DOM affect INP?

It can. When a click or key press changes a large DOM, the style and layout work that follows delays the next frame, which shows up as a slower Interaction to Next Paint. web.dev's DOM size guide covers this case.

Does hiding elements with CSS reduce DOM size?

No. An element with display set to none is still in the DOM and still counted. To shrink the DOM, leave the content out of the HTML and add it when it's needed.