thedaviddias/Front-End-Checklist / skills/dom-size
dom-size
VerifiedUse when auditing slow page loads, heavy assets, or rendering delays related to Reduce DOM size and complexity. Verify the actual bottleneck in DevTools, Lighthouse, or field data before recommending changes.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 1.5 KB |
| references/rule.md | markdown | 4.6 KB |
references/rule.md
View on GitHub# Reduce DOM size and complexity
> Keep the DOM tree small and shallow to improve memory usage and rendering performance.
**Priority:** medium · **Difficulty:** intermediate · **Time:** 10 min
---
The DOM (Document Object Model) is the browser's in-memory representation of your page. As the tree grows, every style recalculation, layout pass, and interaction has more work to do.
## Code Examples
#
## 1. Simplify HTML Structure
Avoid "div-itis" (excessive nesting of `div` elements).
```html
<!-- Bad: Excessive nesting -->
<div class="wrapper">
<div class="container">
<div class="inner">
<p>Hello World</p>
</div>
</div>
</div>
<!-- Good: Clean structure -->
<article class="content">
<p>Hello World</p>
</article>
```
### 2. Implement List Virtualization
Instead of rendering `1,000` rows, only render the visible window plus a small overscan buffer.
```tsx
// Using react-window for a large list
const MyList = ({ items }) => (
{({ index, style }) => (
<div style={style}>
{items[index].name}
</div>
)}
);
```
## Why It Matters
- **Memory Usage**: Every DOM node consumes memory. A massive DOM can lead to browser crashes or slow performance on low-end devices.
- **Style Calculation**: When a CSS class is changed, the browser must re-evaluate styles for all affected nodes. More nodes mean more work.
- **Layout Performance**: Complex DOM trees make "reflows" (re-calculating positions and sizes) much more expensive.
- **Interaction Latency**: Large DOMs can cause lag when scrolling or clicking, negatively impacting metrics like Interaction to Next Paint (INP).
## Diagnose the Real Bottleneck
A large DOM is not always the root problem. Confirm the page is paying for DOM work, not just network cost or oversized JavaScript.
You are likely dealing with a DOM-size problem when:
- Lighthouse reports excessive node count or deep nesting
- DevTools traces show expensive `Recalculate Style` or `Layout`
- Scrolling, filtering, or expanding large views becomes sluggish
- The route renders hundreds or thousands of repeated items at once
## Remediation Order
1. **Remove unnecessary wrappers**: flatten the markup before reaching for more advanced optimizations.
2. **Avoid hidden DOM bloat**: do not keep large panels, menus, or tabs mounted if users cannot see them.
3. **Defer rendering**: mount optional sections, drawers, and modals when needed instead of on first render.
4. **Paginate or virtualize**: when repeated collections remain large, reduce how many rows exist at once.
## Pass-Fail Guidance
- Aim for fewer than roughly `1,500` total nodes on a typical page and a maximum depth below roughly `32`.
- Treat views rendering more than roughly `100-200` repeated items as candidates for pagination or virtualization.
- If traces show layout or style work dominating scroll and interaction, reduce mounted nodes before micro-optimizing components.
## Common Mistakes
- **Optimizing the DOM when the real issue is bundle size**: node count is only one part of the page cost.
- **Keeping hidden UI mounted forever**: invisible nodes still consume memory and style work.
- **Rendering full datasets by default**: feeds, admin tables, and search results often need pagination or windowing.
- **Virtualizing too early**: small lists do not need the complexity.
- **Breaking semantics while flattening markup**: cleaner structure should still preserve headings, lists, and table meaning.
## Tools & Validation
- [Lighthouse](https://developers.google.com/web/tools/lighthouse): Audits the "DOM size" and provides specific counts for nodes and depth.
- **Chrome DevTools Console**: Run `document.querySelectorAll('*').length` to see the current node count.
- **Performance Panel**: Record a trace to see if "Recalculate Style" or "Layout" are taking too long.
## Standards
- Use web.dev: Learn Performance as the standard for measuring the final production behavior, not just local synthetic output.
- Use Chrome Developers: Lighthouse overview as the standard for measuring the final production behavior, not just local synthetic output.
- Use Patterns.dev: List Virtualization as the standard for measuring the final production behavior, not just local synthetic output.
## Verification
### Automated Checks
- Measure the affected page or flow in Lighthouse, PageSpeed Insights, or DevTools and confirm the targeted metric improves.
- Inspect the network waterfall or performance timeline to confirm the intended resource or execution change actually took effect.
### Manual Checks
- Verify the change on a throttled mobile profile, not just local desktop.
- If this rule maps to a budget or Web Vital, confirm the page now stays within that threshold.Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |