Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Perf: propagate outermost/innermost fix to shared dom-utils helpers#154

Merged
twschiller merged 1 commit into
mainfrom
perf/filter-to-outermost-innermost
Jun 5, 2026
Merged

Perf: propagate outermost/innermost fix to shared dom-utils helpers#154
twschiller merged 1 commit into
mainfrom
perf/filter-to-outermost-innermost

Conversation

@twschiller

Copy link
Copy Markdown
Contributor

Follow-up to #151 (Tier 1 perf). That PR fixed the O(C²) outermost-match dedupe inside selector-hide-rule.ts but left the shared filterToOutermost / filterToInnermost helpers in lib/dom-utils.ts on the old candidates.some(other.contains(element)) shape. Those helpers are consumed by hidden-text-strip, prompt-injection-redact, and disguised-ad-flag — so the same fix propagates the perf win across all four call sites.

Summary

lib/dom-utils.ts

  • filterToOutermost: O(C·D). Build a Set of candidate elements once, walk each candidate's parent chain checking Set membership.
  • filterToInnermost: computed as the dual — for each candidate walk up and mark any candidate-ancestors encountered as "has descendant." Anything not marked is innermost. Also O(C·D).
  • Drop the now-unused hasAncestorIn / hasDescendantIn internal helpers — the callers inline the parent walk directly.

lib/selector-hide-rule.ts

Correctness

The existing property tests in dom-utils.property.test.ts exhaustively check the contracts both helpers must satisfy (subset of input, no nested pairs, only-drops-when-related-candidate-exists, idempotence, plus the cross-property intersection check) over random tree shapes and random candidate subsets. Both rewritten helpers pass them unchanged — the algorithmic substitution preserves output exactly.

selector-hide-rule.property.test.ts also covers the rule's scan() integration with the new shared helper.

Test plan

  • bun run test → 1190 passed (unchanged from main — pure refactor)
  • bun run typecheck
  • bun run check (biome + eslint)
  • bun run knip
  • bun run test:coverage — held (statements 85.45%, line/branch/function within ratchet)

🤖 Generated with Claude Code

PR #151 fixed the O(C²) outermost-match dedupe inside selector-hide-rule
but left the shared `filterToOutermost` / `filterToInnermost` helpers in
lib/dom-utils.ts on the old `candidates.some(other.contains(element))`
shape. Those helpers are consumed by hidden-text-strip,
prompt-injection-redact, and disguised-ad-flag — so the same fix
propagates the perf win across all four call sites.

lib/dom-utils.ts:
- filterToOutermost: O(C·D). Build a Set of candidate elements once,
  walk each candidate's parent chain checking Set membership.
- filterToInnermost: computed as the dual — for each candidate walk
  up and mark any candidate-ancestors encountered as "has descendant."
  Anything not marked is innermost. Also O(C·D).
- Drop the now-unused `hasAncestorIn` / `hasDescendantIn` internal
  helpers — their callers inline the parent walk directly.

lib/selector-hide-rule.ts:
- Delete the inline `hasCandidateAncestor` from PR #151. Pre-filter
  candidates via `filterToOutermost(candidates)`, then skip
  non-outermost matches at the top of the placeholder-skip loop.
- Single source of truth: the property tests in
  lib/__tests__/dom-utils.property.test.ts now cover the outermost
  logic everywhere it's used.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@vercel

vercel Bot commented Jun 5, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
agent-browser-shield-demo-site Ready Ready Preview, Comment Jun 5, 2026 4:15am

Request Review

@twschiller twschiller merged commit 6e1f440 into main Jun 5, 2026
7 checks passed
@twschiller twschiller deleted the perf/filter-to-outermost-innermost branch June 5, 2026 04:18
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.

1 participant