Optimize DOM size: find and remove the extra elements

Why Lighthouse shows it grey and only on some runs, where all those elements come from, and the builder, theme and code changes that remove them.

Arjen Karel
Updated · 6 min read
Share

What is Optimize DOM size telling you?

Optimize DOM size, called Avoid an excessive DOM size until Lighthouse 13, means one style calculation or layout of many elements took 40 ms or more during the load. It is grey and unscored: what matters is whether your visitors' taps are slow. Keep one menu instead of a desktop and a mobile copy, and fewer products and reviews per page.

Just want the quick fix? Jump to Elementor and other builders, Shopify or your own code.

When does Lighthouse show Optimize DOM size?

The notice reads: "A large DOM can increase the duration of style calculations and layout reflows, impacting page responsiveness. A large DOM will also increase memory usage." The DOM is your page as the browser holds it: every heading, link and wrapper is an element.

Lighthouse report, Optimize DOM size with a grey empty circle, its description and the label Unscored; a table with Statistic, Element and Value: Total elements 8,670, DOM depth 15 with the path of an icon inside a quantity button, Most children 48 with the product grid ul#product-grid and a thumbnail of the grid

A shoe store's collection page in Lighthouse with DevTools throttling, on a CPU that is really four times slower.

It shows grey when one layout or style recalculation during the load took 40 ms or more on a large part of the page: the browser placing every element and working out its CSS. Otherwise it passes, with the same table under Passed audits.

  • Total elements: every element on the page, the hidden ones too. Most fixes below lower it.
  • DOM depth: the most deeply nested element, often an icon in a button. Rarely worth acting on.
  • Most children: the element with the most elements directly inside it, a product grid or the body with its popups.

There is no element limit. The 800 and 1,400 in older articles were the old audit's scoring, and 1,500 elements, a depth of 32 and 60 children come from Lighthouse 5 and older.

Whether a layout reaches 40 ms depends on the machine. The shoe store's page was grey in every run with DevTools throttling and passed every run with simulated throttling, PageSpeed Insights' method, which records the page at full speed (why runs differ).

Unscored: your Performance score stays the same, but Google ranks on your real visitors' numbers, and a large DOM can make their taps slower.

What does a large DOM do to your visitors?

A shopper taps Filter and sort, and the drawer opens a moment later. Before the next frame, the browser restyles and lays out every element a change touches, and a CSS variable on <html> touches them all. Interaction to Next Paint (INP) counts that wait, as presentation delay or inside the click handler. Hidden copies and off-screen sections under content-visibility take little of that work, but Lighthouse counts them.

On the shoe store's page, built like a typical Shopify theme (the menu twice, 48 cards with a quick view and a size picker in each, 36 reviews in the page), with the CPU four times slower:

Collection pageElementsLongest layout during the loadNoticeFilter and sort tap
Before8,67049 msgrey48 ms
After the fixes below1,90924 mspassed32 ms
Before with content-visibility8,67018 mspassed32 ms
Chrome DevTools Performance panel, a tap on Filter and sort on the shoe store's page before the fix: the Interactions track shows a Pointer interaction with Input delay 19 ms, Processing duration 19 ms and Presentation delay 12 ms, the sidebar shows INP 50 ms and the INP breakdown, and in the Main track a Function call with a long Recalculate style bar under it

The Filter and sort tap before the fix: Recalculate style fills most of the click handler.

Every tap stayed under Google's 200 ms limit here; a cheap phone is slower still. According to the State of Web Vitals, home pages with more than 3,000 DOM elements pass INP on phones less often than home pages with 400 to 800 (84% against 95%), even among sites with the same amount of JavaScript, page weight, third parties, server speed and platform. That is a correlation.

Do you need to fix Optimize DOM size?

Check INP for real visitors on mobile, on the pages that show the notice (field data). If INP is good there and the notice only shows now and then, leave it. Fix it when INP needs improvement or is poor, or when copies and long lists add thousands of elements. A consent banner has to be there, so leave its elements.

Which part of your page adds all those elements?

Record the load in the DevTools Performance panel with the CPU four times slower: unlike Lighthouse, the Insights sidebar names the layout or style recalculation that turned the notice grey, with its duration.

Chrome DevTools Performance panel with the load of the shoe store's page: in the Insights sidebar Optimize DOM size expanded with Total elements 8670, DOM depth 15, Most children 48, the elements path and UL id product-grid, and under These are the largest layout and style recalculation events the row Layout (7414 objects) 49 ms; in the Main track a long purple Layout bar after Recalculate style

The shoe store's load in DevTools.

Count per part: select the header or the product grid in the Elements panel and type $0.querySelectorAll('*').length in the Console. On a staging copy, switch off one plugin, app or section at a time and recount. The usual places:

  • Builder wrappers: several per widget, inside a column, inside a section.
  • Two menus: a desktop menu and a hidden mobile copy, hundreds of elements with a mega menu.
  • Filters twice: a desktop sidebar and the same filters in a drawer for phones.
  • Apps and reviews: every review in the page, popup and upsell apps.
  • Consent banners: the preferences panel with its vendor list, often the largest hidden block.
  • Cards, icons and sliders: a quick view and a size picker in every card, two elements or more per inline SVG icon, every slide.

How do you fix Optimize DOM size?

Start with the largest part you control, copies first: they add elements and show your visitor nothing.

Elementor and other WordPress builders

  • Containers. A container is one wrapper; a section with a column is several. Turn Container on (Elementor, Editor, Settings, Features), Convert each section under Layout, and remove inner sections. Back up first: converting is one-way.
  • Optimized Markup, in the same place, removes the extra wrapper inside most widgets. Then check add-ons and custom CSS that target .elementor-widget-container.
  • One element for every screen. Hide On (Advanced, Responsive) hides with CSS, so both copies stay: build one header that adapts. Avada's device visibility does the same.
  • Inline Font Icons draws each icon as SVG: fewer files, more elements.
  • Gutenberg: no Group inside a Group, one Navigation block.
  • WooCommerce: fewer Rows per page (Customizer, WooCommerce, Product Catalog) or the Product Collection block's Products per page.

WP Rocket's Automatic Lazy Rendering, on by default, uses content-visibility: the notice can pass with Total elements unchanged, which is right. NitroPack's HTML Lazy Load adds sections as the visitor scrolls: the count drops, and the browser does that work during the scroll.

What undoes it: an imported template kit with sections, a new section with Hide On.

Shopify

  • One menu. In Dawn's Header, Menu type Drawer puts the menu in the page once; Dropdown and Mega menu add a drawer copy.
  • One filter panel: Filter layout Drawer in the collection's Product grid section.
  • Fewer products: Products per page in the same section, Quick add off if nobody uses it, and Horizon's Auto load products on scroll off: it keeps adding them.
  • Review apps: fewer reviews per page (Reviews per page in Judge.me and Yotpo, Number of reviews to display in Loox), a summary on collection pages. An app that sells stays; know what it adds.
The shoe store's collection page on a phone before the fix, at the end of the product grid: Showing 48 of 86 products, a Load more button, the reviews block What runners say with 4.6 stars based on 1,284 reviews, the score bars and below them the first full review card by Sanne K.

Before

The same place after the fix: Showing 24 of 86 products, a Load more button, the same reviews summary with 4.6 stars and the score bars, then a Read all 1,284 reviews button and the shop's free shipping and 60-day returns blocks

After

The end of the grid on a phone: 48 products and 36 reviews, or 24 products and a link to the reviews.

A theme update or a mega menu block added later undoes it.

Your own code

  • One menu, restyled per breakpoint.
  • Build on first open: the drawer, the quick view and the size picker, from the card's data. Keep the menu's links in the page once for search engines, with aria-expanded on the button.
  • Pagination with real links: Load more as a link to page 2, because Google follows links.
  • An SVG sprite: one <svg><use> per icon instead of its paths in every card.
  • Virtualise long lists in a single-page app with react-window or TanStack Virtual.
  • content-visibility below the first screen: those sections render when they come near (the last row of the table).
/* cards after the first four, and the long sections under the grid */
.product-card:nth-child(n+5) {
  content-visibility: auto;
  contain-intrinsic-block-size: auto 420px;
}
.reviews, .site-footer {
  content-visibility: auto;
  contain-intrinsic-block-size: auto 900px;
}

The height is your guess for a phone, and auto lets the browser remember the real one; a guess far off makes the scrollbar jump. Sticky elements and dropdowns inside get clipped: leave those sections out.

Deleting the desktop menu with JavaScript after the load is too late: the browser has done the work. For a script that measures every card after a change, see Forced reflow.

How do you know the fix worked?

Run Lighthouse again with the same throttling. The notice comes and goes, so also compare recordings of the load and the same tap: the layout and Recalculate style should be shorter.

CoreDash, Interaction to Next Paint by Urls on acme-pagespeed.com, grouped by URL: per URL the p75 INP, the label good, the distribution bar, impact, a Trend column with arrows and percentages, and the number of sessions, with the collection pages for running shoes and trail shoes among the URLs

CoreDash, Interaction to Next Paint by Urls on mobile.

For real visitors, find the pages that showed the notice and watch their Trend after the change. Search Console takes up to 28 days to show it.

When the elements come from a theme or builder nobody can rebuild without breaking pages, or from an app whose markup you can't change, a power hour is the next step: we record your slowest tap together and pick what to rebuild first.

About the author

Arjen Karel is a web performance consultant and the creator of CoreDash, a Real User Monitoring platform that tracks Core Web Vitals data across hundreds of sites. He also built the Core Web Vitals Visualizer Chrome extension. He has helped clients achieve passing Core Web Vitals scores on over 925,000 mobile URLs.

Arjen Karel

Need a faster site?

I'm Arjen, and I've been fixing slow sites for 17 years. If you're running into issues you can't fix yourself, send me your URL and let's have a chat.

Send me your URL
Arjen Karel
Arjen Karel, Web performance consultant since 2009

Why isn't our fix working?

Sometimes the CDN still serves the old HTML, or the fix went into the desktop template and mobile uses another one. Send me your URL and the problem, and I reply with a few times. I check your field data first, then we spend one hour on your site, and within a day you have my notes in writing. €350 excl. VAT.

Send me your URL

No developer to build the fix? I do the work myself, on a copy of your site.

Arjen KarelCore Web Vitals still red on mobile?Send me your URL