Skip to content

fix(rules): perf/dom-size measures total nodes and depth from body #548 - #886

Merged
nc9 merged 1 commit into
mainfrom
fix/548-dom-size-body-scope
Oct 17, 2026
Merged

nc9 merged 1 commit into
mainfrom
fix/548-dom-size-body-scope

Conversation

@nc9

@nc9 nc9 commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

Closes #548

The decision is recorded on the issue before the change (#548 (comment)): body-only, for all three checks.

What changed

  • perf/dom-size measures total nodes and depth from <body>, as dom-max-children has since perf/dom-size: dom-max-children counts the children of <head> #483 and as Lighthouse's DOM size audit does. Lighthouse's DOMStats gatherer walks document.body, counts the body's descendants (totalBodyElements) and puts the body at depth 1. This rule now does the same: the total excludes <body> itself, and <body> is level 1.
  • <head> metadata (hreflang alternates, preload hints, meta tags) no longer adds to the count or the depth.
  • Thresholds are reviewed and unchanged: 1,500 / 3,000 nodes, 32 levels, 60 children. Body-only lowers a typical count by the head size plus 2 (roughly 20 to 100 nodes), which is small next to the 1,500 line the rule's guidance quotes. Lighthouse's own lines are lower (800 / 1,400); moving the defaults would be a separate scoring change.
  • A document with no <body> gets one skipped dom-total-nodes check (No body element found). A rootless document (empty, text-only or comment-only HTML, where linkedom's body getter throws) keeps No document root found.
  • Lighthouse mapping: apps/cli/src/benchmark/mapping.ts pairs LH dom-size with perf/dom-size by id. That still holds, and the two now measure the same scope.

Acceptance criteria

  • Choice recorded on the issue before the change
  • Total nodes and depth measured from <body>; a large head with a small body reports the body numbers
  • Thresholds reviewed (unchanged, reasoning above); docs state the thresholds and what is counted
  • The three checks agree on what they measure
  • Test: 70-link head with a small body: total nodes 3, depth 3 (and a 50-node warn threshold the head alone would cross still passes)
  • Test: a deep body is reported at the threshold (32 passes, 33 warns)
  • Benchmark mapping still matches
  • Skill: no change needed.

Tests

packages/rules/tests/dom-size.test.ts 10/10, including rootless real-parse cases. Golden suite: 209/210. The one failure is the same 180 s timeout in rule-cache-replay-golden ("a page where a rule timed out is not stored") that showed up in full-suite runs on two other branches; that file passes alone (11/11, 15 s). No pinned number moved. Typecheck (rules), oxlint on changed files, tangly check --strict, codex review (2 rounds; the P2 rootless-document guard is fixed).

All three checks now walk <body>, as dom-max-children did and Lighthouse's
DOMStats does: total = body descendants, body = depth 1. Thresholds unchanged.
Rootless documents stay a skipped check.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the fix/548-dom-size-body-scope branch from 0456cb5 to f61d4db Compare October 17, 2026 01:14
@nc9
nc9 marked this pull request as ready for review October 17, 2026 01:14
@claude

claude Bot commented Oct 17, 2026

Copy link
Copy Markdown

Reviewed the diff. The change does what the description says: total nodes and depth are now measured from <body>, the total excludes <body> itself, and <body> is depth 1. The countNodes(body) - 1 arithmetic checks out against the helpers. The no-root guard runs before the document.body access, so linkedom's throwing getter is handled. The tests cover the threshold edges (1499/1500/3000, depth 32/33), the large-head case, and the rootless and no-body documents.

  • (low) packages/rules/src/performance/dom-size.ts:103: countNodes(body) - 1 makes the helper count self and then subtract it back out. That is correct but easy to misread later. A short comment, or summing over body.children, would make it clearer.
  • (nit) packages/rules/src/performance/dom-size.ts: countNodes and calculateMaxDepth are recursive. A pathologically deep body could overflow the stack, but this was already true before the PR and is out of scope here.
  • (nit) The behaviour change is intended and documented in the changelog: counts drop by roughly the head size plus 2. Pages that sat just above 1,500 may flip from warn to pass, so it may be worth mentioning in the release notes.

Nothing blocking.

@nc9
nc9 merged commit df76681 into main Oct 17, 2026
16 checks passed
@nc9
nc9 deleted the fix/548-dom-size-body-scope branch October 17, 2026 01:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf/dom-size: scope total node count and depth consistently with dom-max-children

1 participant