Read the last token of a selector that ends in a nested pseudo-class - #182
Merged
Conversation
jdalton
force-pushed
the
perf/optimizer-nesting
branch
3 times, most recently
from
September 5, 2026 18:56
14c3853 to
ec74648
Compare
This was referenced Sep 5, 2026
jdalton
force-pushed
the
perf/optimizer-nesting
branch
2 times, most recently
from
September 6, 2026 02:30
4678dac to
463fe40
Compare
Before testing candidates, collect() asks reOptimizer for the last simple token of a selector and uses it to fetch the candidates by tag, class or id. The parenthesized part of that pattern is '\x28[^\x29]+(?:\x29|$)', which stops at the first ')', so a final compound holding a nested functional pseudo-class does not match at all — and a selector the optimizer cannot read is answered by walking every element in the context. 'div:not(:nth-of-type(2n))' therefore tests every element in the document instead of the divs, and since ':not()' evaluates its argument through s.match() per element, each of those elements resolves nth-of-type. On a 6300-element page that is 6344 resolutions building 3911 sibling caches over 196312 steps, for a selector whose subject is a div. The parenthesized part now tolerates two levels of nesting, which reaches ':not(:not(:not(span)))'. Deeper than that falls back to the unoptimized scan, as before. Both the old and new patterns stay linear on unbalanced input: 3200 unclosed parentheses match in 0.02ms. div:not(:nth-of-type(2n)) 45.62ms -> 134.92us 338x div:not(:nth-child(3)) 9.42ms -> 123.94us 76x div:is(.example):not(:where(.x)) 2.65ms -> 39.45us 67x div:not(.x) 27.93us -> 27.75us - Results are unchanged; the four above agree with the native engine. References: - Spec: https://drafts.csswg.org/css-syntax/#consume-simple-block — why a parenthesized part has to tolerate nesting - Spec: https://drafts.csswg.org/selectors-4/#matches — the functional pseudo-classes that put parentheses inside a compound
jdalton
force-pushed
the
perf/optimizer-nesting
branch
from
September 6, 2026 11:40
463fe40 to
441874e
Compare
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.
Tooling update
Rebased onto merged #212 as one commit. Sources and tests use
.mts, Node tests use Vitest, and the published JavaScript paths stay unchanged. The original engine changes are preserved.Local lint, formatting, Node and packed-package checks pass. All 41 WPT pages pass against both generated builds.
Change
Read the final compound when a selector ends in nested functional pseudo-classes. This narrows the candidate list instead of scanning every element.
Extracted from #167. The parser variable is local, so strict factory initialization works without creating a global. Nesting beyond the pattern's supported depth retains the unoptimized fallback.
All six regression tests pass: strict initialization and five nested selector shapes, including cached queries. Run with Node.js ≥ 22 using
pnpm install && pnpm run test:optimizer.The original aggregate benchmark measured
div:not(:nth-of-type(2n))at 45.62 ms before and 134.92 µs after. Those historical timings were not rerun for this repair.References: consume a simple block, logical combination pseudo-classes.