Perf: trailing-only throttle, burst flush, O(C^2) outermost-match fix (#150 Tier 1)#151
Merged
Merged
Conversation
Tier 1 of the perf plan in #150 — plumbing wins, no architectural change. Each item targets a measured hotspot in the watcher / hide-rule loop that fires on every infinite-scroll burst and SPA route swap. lib/subtree-watcher.ts: - Trailing-only throttle. Previously `leading: true, trailing: true` ran drain at both edges of every window, so each burst scanned twice. Now coalesces to one drain at the end. - Burst-size flush. When pending grows past 512 roots (e.g. a React route swap dumping thousands of nodes in one microtask), cancel the timer and drain immediately so the user-visible hide doesn't wait out 250ms of throttle. Pattern from Ghostery's DOMMonitor. - ignoreTags filter at enqueue. Drop <style> and <br> additions before they hit the pending set — never interesting to any rule. Kept conservative: <script> stays through because json-ld-sanitize / schema-trust-sanitize observe <script type="application/ld+json"> insertions. - visibilitychange pause. Disconnect the observer when document.hidden is true (background tab), flush pending so we don't sit on a stale snapshot, reattach + drain on visible. Stops background tabs from burning CPU on observer callbacks while the user isn't looking. lib/selector-hide-rule.ts: - Memoize selectorsFor(url). Single-entry cache keyed by globalThis.location.href so the URLPattern.test loop only runs on route changes. Defensive-copy on return; internal scan() uses the cached joined selector string. - Fix O(C²) outermost-match. Replace the `candidates.some(other => other.contains(element))` filter with a Set + parentElement walk. O(C·D) instead of O(C²) — on a feed where C grows into the hundreds over a scroll session this stops being the dominant cost in scan(). Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This was referenced Jun 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Tier 1 of #150 — plumbing wins inside
lib/subtree-watcher.tsandlib/selector-hide-rule.ts. No architectural changes; the watcher API and rule shapes are unchanged.Summary
lib/subtree-watcher.tsleading: true, trailing: true— every burst drained twice. Now coalesces to one drain at the end of the window.DOMMonitor.IGNORE_TAGSfilter at enqueue. Drop<style>and<br>additions before they hitpending. Kept conservative:<script>stays through becausejson-ld-sanitizeandschema-trust-sanitizeobserve<script type="application/ld+json">additions.visibilitychangepause. Disconnect the observer whiledocument.hidden, flush pending so we don't sit on a stale snapshot, reattach on visible. Stops background tabs from running observer callbacks.lib/selector-hide-rule.tsselectorsFor(url). Single-entry cache keyed byglobalThis.location.hrefso theURLPattern.testloop only runs on route changes. Internalscan()reuses the cached joined selector string; publicselectorsFor()returns a defensive copy.candidates.some(other => other.contains(element))with aSet<HTMLElement>+parentElementwalk. O(C·D) instead of O(C²) — matters on feeds where C grows into the hundreds over a scroll session.Tier 1 items not in this PR
Tier 1 in #150 also listed
ignoreTagsat the watcher level (done — minus<script>to stay safe) and avisibilitychangepause (done). All six Tier-1 items are landed here.Test plan
bun run test→ 1164 passed (up from 1146 — 18 new tests covering trailing-only behavior,IGNORE_TAGS, burst flush, visibility pause)bun run typecheckbun run check(biome + eslint)bun run knipbun run test:coverage— well above ratchet (statements 85.38%, branches 76.77%, functions 88.53%, lines 85.28%)Refs #150.
🤖 Generated with Claude Code