Skip to content
Learn Measure Blog Case studies About
On this page
  • How the Lighthouse DOM size audit fails
  • How to optimize the DOM size
  • Stack-specific guidance
    • Angular
    • React
  • Resources

Avoid an excessive DOM size

May 2, 2019 — Updated Oct 4, 2019
Available in: Español, 日本語, 한국어, Português, Русский, 中文, English
Appears in: Performance audits
On this page
  • How the Lighthouse DOM size audit fails
  • How to optimize the DOM size
  • Stack-specific guidance
    • Angular
    • React
  • Resources

A large DOM tree can slow down your page performance in multiple ways:

  • Network efficiency and load performance

    A large DOM tree often includes many nodes that aren't visible when the user first loads the page, which unnecessarily increases data costs for your users and slows down load time.

  • Runtime performance

    As users and scripts interact with your page, the browser must constantly recompute the position and styling of nodes. A large DOM tree in combination with complicated style rules can severely slow down rendering.

  • Memory performance

    If your JavaScript uses general query selectors such as document.querySelectorAll('li'), you may be unknowingly storing references to a very large number of nodes, which can overwhelm the memory capabilities of your users' devices.

How the Lighthouse DOM size audit fails #

Lighthouse reports the total DOM elements for a page, the page's maximum DOM depth, and its maximum child elements:

A screenshot of the Lighthouse Avoids an excessive DOM size audit

Lighthouse flags pages with DOM trees that:

  • Warns when the body element has more than ~800 nodes.
  • Errors when the body element has more than ~1,400 nodes.
See the Lighthouse performance scoring post to learn how your page's overall performance score is calculated.

How to optimize the DOM size #

In general, look for ways to create DOM nodes only when needed, and destroy nodes when they're no longer needed.

If you're currently shipping a large DOM tree, try loading your page and manually noting which nodes are displayed. Perhaps you can remove the undisplayed nodes from the initially loaded document and only create them after a relevant user interaction, such as a scroll or a button click.

If you create DOM nodes at runtime, Subtree Modification DOM Change Breakpoints can help you pinpoint when nodes get created.

If you can't avoid a large DOM tree, another approach for improving rendering performance is simplifying your CSS selectors. See Google's Reduce the Scope and Complexity of Style Calculations for more information.

Stack-specific guidance #

Angular #

If you're rendering large lists, use virtual scrolling with the Component Dev Kit (CDK).

React #

  • Use a "windowing" library like react-window to minimize the number of DOM nodes created if you are rendering many repeated elements on the page.
  • Minimize unnecessary re-renders using shouldComponentUpdate, PureComponent, or React.memo.
  • Skip effects only until certain dependencies have changed if you are using the Effect hook to improve runtime performance.

Resources #

  • Source code for Avoid an excessive DOM size audit
  • Reduce the Scope and Complexity of Style Calculations
Memory
Last updated: Oct 4, 2019 — Improve article
Return to all articles
Share
subscribe

Contribute

  • File a bug
  • View source

Related content

  • developer.chrome.com
  • Chrome updates
  • Web Fundamentals
  • Case studies
  • Podcasts
  • Shows

Connect

  • Twitter
  • YouTube
  • Google Developers
  • Chrome
  • Firebase
  • Google Cloud Platform
  • All products
  • Terms & Privacy
  • Community Guidelines

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies.