Selector cache lifecycle
Selector caching reduces repeated work while a compiled selector predicate is used against one document state. The cache belongs to the predicate that created it. It does not provide a global lookup from selector text to compiled functions, so callers must keep and reuse a compiled predicate when they want reuse across queries.
Cache scope
The selector engine creates a CompiledQuery function from selector tokens and query options. A compiled function may close over a result cache for expensive parent-dependent checks, but each compilation creates a separate cache scope. The parent page, Query execution and caching, describes how this fits into query execution; Query traversal describes how the cached predicate is applied to candidate nodes.
Compiled predicate identity
The effective cache key is the identity of an element node, not the selector string. The cache is attached to the matcher closure created during compilation, so two compilations of the same selector do not share entries. Context, adapter behavior, and other options therefore remain part of the predicate that owns the cache rather than a separate cache-key record.
References: src/index.ts• L42-79 src/types.ts• L185-206 src/…/cache.ts• L18-30
Result cache storage
The result store is a WeakMap from input nodes to boolean match results. Weak node keys let the store follow the lifetime of the nodes without a separate cleanup step. If the runtime has no WeakMap, or if cacheResults is false, the matcher evaluates the wrapped check directly.
References: src/…/cache.ts• L18-45 src/types.ts• L166-171
Cache reuse
The cache targets checks where a match for a parent can inform checks for nodes in its subtree. This is useful for content and nested-selector conditions such as :contains and non-relative :has, where repeated candidate checks can inspect overlapping descendants.
Parent result reuse
When a candidate is tested, the matcher first applies the next predicate in the compiled chain. It then checks the candidate cache entry. If no entry exists, it walks toward the element parent until it finds a cached result or reaches the root. A cached negative parent result rejects the candidate without another expensive check. A positive parent result still permits a result for the candidate to be stored.
References: src/…/cache.ts• L39-61 src/…/querying.ts• L140-151
Nested selector reuse
Nested selectors are compiled as part of the outer predicate. A non-context-dependent :has check can use the parent-result cache because its answer does not depend on the current query element. A relative nested selector or one that contains :scope cannot use that reuse safely, so it evaluates against the current element instead.
References: src/…/subselects.ts• L20-34 src/…/subselects.ts• L72-139
Cache invalidation
The cache has no explicit invalidation operation. Its entries remain valid only while the node relationships and values observed by the compiled predicate remain unchanged. This is why cacheResults should be disabled when the document can change between queries that reuse the same compiled selector.
Document mutation boundary
Changes to text, attributes, parent links, or child lists can change the answer represented by an existing entry. The engine does not detect those changes. The safe choices are to compile or invoke a predicate for a stable document state, or set cacheResults: false when callers cannot provide that boundary.
References: src/types.ts• L166-171 src/…/cache.ts• L27-37
Context-sensitive predicates
Selectors that start with traversal or contain :scope depend on the current context. The compiler marks these cases so that parent-result caching is skipped. This keeps a result tied to the context in which it was computed instead of reusing it for a different candidate context.
References: src/…/subselects.ts• L20-34 src/…/subselects.ts• L84-90 src/…/subselects.ts• L97-139
Change checklist
When these files or sections are changed, remember to consider these:
- Preserve the rule that cache entries belong to one compiled predicate and one node identity space. A shared cache must also account for adapter behavior, context, and selector options.
- Keep
cacheResultsbehavior aligned with the documented mutation boundary. A new cached selector must not reuse results when its answer depends on query context or mutable state. - If nested-selector context rules change, review both the
:hascache decision and the sibling preparation signaled byshouldTestNextSiblings.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The document changes while a cached compiled predicate is reused | A stored result can no longer describe the current node tree or content | Set cacheResults: false or use a new compiled predicate after mutation |
| A context-dependent nested selector enters the parent-result cache | A result from one scope can affect a different scope | Keep traversal-starting and :scope selectors on the non-cached path |
The runtime does not provide WeakMap | Parent-result reuse is disabled and the wrapped check runs for each candidate | Treat this as a performance fallback, not as a change in match semantics |
Key files
| File | Why to read it |
|---|---|
| src/…/cache.ts | Defines the node-keyed result store and its parent reuse behavior. |
| src/…/subselects.ts | Shows which nested selectors can use the cache and which require current context. |
| src/index.ts | Shows how callers create compiled predicates and pass them into selection operations. |
| src/types.ts | Defines the cache option and the compiled predicate contract. |