Query execution and caching
The query engine turns a CSS selector into a reusable element matcher and applies that matcher while it walks a node tree. It improves repeated selection by reusing compiled matchers and memoizing safe intermediate results for stable parts of the tree.
Query execution
The public selection operations accept a selector string or a compiled matcher. They normalize the query context, account for sibling-dependent selectors, and then search for either all matches or the first match. The adapter supplies the tree operations, so the same query model can work with different node types. See query traversal for the traversal flow.
Query preparation
Query preparation converts selector text into an executable predicate and reduces the input to the roots that need inspection. A compiled predicate also records when a selector needs following siblings, which lets the search context include the nodes required by sibling combinators. References: src/index.ts• L82-149, src/compile.ts• L69-115, src/types.ts• L143-164
Tree traversal
Tree traversal visits element nodes in depth-first order and applies the predicate at each visited element. The all-match form continues through the tree, while the first-match form stops as soon as it finds a match; non-XML queries do not descend into template contents. References: src/…/querying.ts• L3-60, src/…/querying.ts• L62-118, src/index.ts• L161-198
The following diagram shows how a selection moves from a selector to its result set.
flowchart LR
A[Selector or compiled matcher] --> B[Normalize context]
B --> C[Compile selector if needed]
C --> D[Traverse element tree]
D --> E[Apply matcher]
E --> F[All matches or first match]
Selector result reuse
Compilation builds a chain of checks for selector rules and orders inexpensive checks before costly checks when that is safe. The resulting function can be passed to later selection calls, so parsing and selector setup do not need to repeat for each query.
Cache eligibility
The engine caches only results that do not depend on the current element as a local scope. Relative traversals and :scope make a nested selector context-dependent, so the engine evaluates those checks for each candidate instead of reusing a result from another context. References: src/…/selectors.ts• L15-45, src/…/selectors.ts• L78-124, src/…/subselects.ts• L20-34, src/…/subselects.ts• L72-139
Cache scope
Eligible results are stored against nodes with weak node-keyed caches owned by the compiled matcher. Descendant checks and parent-based checks can then reuse a known result for a subtree or its ancestors. This cache is enabled by default, can be disabled with cacheResults, and must not be used when the document changes between calls with the same compiled selector. See selector cache lifecycle for cache ownership, validity, and invalidation guidance. References: src/…/cache.ts• L4-61, src/general.ts• L84-132, src/types.ts• L166-171
Traversal and cache relationship
Traversal determines which elements are candidates; caching reduces repeated work while evaluating candidates. A cached matcher does not replace tree traversal, and a cache cannot safely cross a context change or a document mutation that can alter the selector result.
Nested selector evaluation
Selectors such as :has evaluate another selector inside an element's children. When that nested selector is context-independent, the engine can reuse the parent result across matching candidates; when it depends on traversal or scope, it performs a fresh local search. References: src/…/subselects.ts• L72-139, src/…/cache.ts• L18-61
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to adapter parent, child, sibling, or equality behavior affect both traversal results and node-keyed cache lookups.
- Changes to relative-selector handling must preserve the link between
shouldTestNextSiblingsand context preparation. - New nested selector behavior must classify scope-dependent selectors correctly before enabling result reuse.
- Changes to document mutability assumptions must update the
cacheResultscontract and the selector cache lifecycle guidance.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The document changes after a compiled matcher caches results | A later selection can return results from the old tree state | Set cacheResults: false when the same compiled selector runs across mutations |
A relative selector is used while relativeSelector is disabled | Compilation throws instead of executing the query | Keep relative selectors enabled or provide an absolute selector |
| An adapter reports incorrect parents or children | Traversal and cached ancestor results can both be wrong | Keep the adapter's tree relations consistent for the full query lifetime |
| A selector needs following siblings but the context omits them | Sibling-dependent matches can be missed | Use the public selection path so it can prepare the sibling context |
Key files
| File | Why to read it |
|---|---|
| src/index.ts | Defines the public compile, select, and match flow. |
| src/compile.ts | Shows how selector structure becomes a matcher and how context needs are marked. |
| src/…/querying.ts | Defines the depth-first all-match and first-match searches. |
| src/general.ts | Connects selector checks and traversal relations, including descendant result reuse. |
| src/…/cache.ts | Defines parent-result memoization and its opt-out behavior. |
| src/…/subselects.ts | Shows how nested selectors such as :has choose between local evaluation and caching. |
| src/types.ts | Documents the adapter, query, context, and cache option contracts. |