Skip to content

perf(rules): one-pass scan in legal/cookie-consent, no lowercase copy in social/share-buttons #270 - #1008

Merged
nc9 merged 1 commit into
mainfrom
perf/270-rules-phase-hotspots
Oct 22, 2026
Merged

nc9 merged 1 commit into
mainfrom
perf/270-rules-phase-hotspots

Conversation

@nc9

@nc9 nc9 commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Closes #270

What

  • legal/cookie-consent: the 21 querySelector and 7 querySelectorAll full-document sweeps become one pass over the elements. The predicates are the selector engine's own: id compared verbatim, class by whole whitespace-delimited token (both case-sensitive), cookie/consent by case-insensitive RegExp on the raw attribute, role/aria-modal exact.
  • social/share-buttons: the four /i patterns are tested on the raw page instead of html.toLowerCase(). The two differ only where toLowerCase() produces an ASCII letter from a non-ASCII one (U+212A Kelvin sign, U+0130), so a page containing either keeps the lowercased path. Answers are unchanged, so no decision is needed on the divergence.

Numbers

Rule-level bench on a generated 1.03 MB script-heavy page (59 inline scripts, 1,500 cards), 60 run() calls after warm-up, base and branch alternating, 5 rounds, medians, isolated HOME, wall time:

rule before after
legal/cookie-consent 17.95 ms/page 2.78 ms/page
social/share-buttons 3.08 ms/page 2.49 ms/page

The original 1.13 MB fixture is not in the repo, so these are not comparable to the 8.2 / 6.4 ms in the issue. Results of both rules are byte-identical on the fixture. Row added to benchmarks/2026-09-perf-program.md. share-buttons is mostly its four regexes now; the saving there is the lowercase copy.

Parity

packages/rules/tests/cookie-share-single-pass-270.test.ts: 2,000 randomised pages (case variants, tab/NBSP/newline class separators, Kelvin sign) compared against the verbatim old selector logic; every BMP character spliced into each share pattern compared against the lowercase version. Both mutation-checked (dropping the Kelvin fallback and making the cookie regex case-sensitive each fail the suite). cookie-consent-detection.test.ts passes. tsgo --noEmit clean in packages/rules.

Docs and skill

No docs change (no behaviour change). Agent skill: no change needed.

@claude

claude Bot commented Oct 22, 2026

Copy link
Copy Markdown

Reviewed the diff and tests by reading only; I did not run the tests or check CI. No blocking issues found.

  • (low) cookie-consent.ts scanElements: cls.split(/\s+/) splits on all Unicode whitespace (NBSP, U+FEFF), but a .cc-window selector only splits class tokens on ASCII whitespace. A class like "x\u00a0cc-window" would now match where the old selector did not. Very unlikely in practice. For exact parity use /[ \t\n\f\r]+/. The randomized parity test has no non-ASCII whitespace in CLASSES, so it can't catch this; adding one would settle it.
  • (nit) The early return { container: true, regions } returns a partial regions list. Harmless because the caller ignores it, but returning [] or adding a comment would be clearer.
  • (nit) el as Element is probably redundant if querySelectorAll("*") is already typed as Element.
  • (nit) The BMP sweep test (about 65k code points x 8 seeds x several positions x 4 regexes, 60s timeout) could be flaky on slow CI. Consider limiting it to non-ASCII code points that toLowerCase() changes, plus a sample of others.

The share-buttons guard for U+212A and U+0130 looks correct and is well covered by the exhaustive test. The benchmark notes and changelog entry are clear.

Co-Authored-By: Claude Sonnet 5.5 [email protected]

… in social/share-buttons #270

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the perf/270-rules-phase-hotspots branch from f939973 to 115aeb4 Compare October 22, 2026 01:43
@claude

claude Bot commented Oct 22, 2026

Copy link
Copy Markdown

Review of #1008 (single-pass scan for legal/cookie-consent, un-lowercased patterns for social/share-buttons). I read the diff only and did not run the tests.

  • (low) packages/rules/src/legal/cookie-consent.ts, WHITESPACE_RE = /\s+/: JS \s also matches NBSP, U+2028/2029, U+FEFF and other Unicode spaces. Class tokenisation in selectors splits only on ASCII whitespace (space, tab, LF, FF, CR). A class such as cc-window followed by an NBSP and x is one token to the old .cc-window selector, but would match here. That contradicts the identical-answers claim. Use /[ \t\n\f\r]+/. The randomised test's class corpus has no Unicode whitespace, so it would not catch this. Please add a case.
  • (nit) The el as Element cast in scanElements is probably redundant now.
  • (nit) CMP_IDS and CMP_CLASSES are hand-split copies of the old selector list. The test keeps the old list verbatim, which is good, but a comment that the two must stay in sync would help.

Otherwise the early return on a container hit is safe because regions is unused in that case. The Kelvin sign and U+0130 fallback in share-buttons looks correct, and the exhaustive BMP test covers it well. The benchmark write-up is clear.

@nc9
nc9 merged commit 5b0ea47 into main Oct 22, 2026
15 checks passed
@nc9
nc9 deleted the perf/270-rules-phase-hotspots branch October 22, 2026 02:13
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.

legal/cookie-consent and social/share-buttons are now the top of the rules phase on script-heavy pages

1 participant