Skip to content

perf(querying): use push/pop stacks in findAll and findOne - #1944

Open
g4lb wants to merge 1 commit into
fb55:masterfrom
g4lb:perf-querying-stack
Open

g4lb wants to merge 1 commit into
fb55:masterfrom
g4lb:perf-querying-stack

Conversation

@g4lb

@g4lb g4lb commented Nov 15, 2026 •

Copy link
Copy Markdown

findAll and findOne called unshift and shift on two stacks to walk the tree. This change keeps the current array and index in local variables. It uses push and pop only when the walk goes into a child array or comes back from it. The visit order and the results do not change.

dist build, ns/op:

Benchmark Before After Change
selectAll("div") on a page with 2,083 elements 169,443 50,214 -70.4%
selectAll("ul > li > a") on the same page 172,039 55,323 -67.8%
4 attribute selectors on the same page 790,034 282,246 -64.3%
selectOne with no match on 4,000 elements 161,956 66,716 -58.8%
5,000 sibling elements 431,378 181,221 -58.0%
1,500 nested elements 399,331 37,250 -90.7%
3 queries on test/fixtures/sizzle.html 83,284 54,874 -34.1%
400 separate root elements 239,565 198,499 -17.1%

I measured no slowdown on 33 benchmarks. These include text-heavy documents, <template> content, XML mode, a tiny document, and selectOne with an early match. I used 12 interleaved rounds and a Mann-Whitney test (Apple M2 Max, Node 24.15.0).

Checks:

  • npm test passes.
  • Old and new outputs matched on 1,000,000 random cases in Node 24. Each case compares the results and errors of selectAll, selectOne, is, and compile. The cases use random documents, options, and adapters without prevElementSibling. The selectors are random, or they come from the 1,127 selector strings in test/.
  • Old and new outputs also matched on 100,000 cases each in Node 22.12, Bun 1.3.14, and Deno 2.9.1. I did not test Node 20.

View guided diff

Summary by CodeRabbit

  • Bug Fixes
    • Updated node traversal order to depth-first while preserving search results and termination behavior. Template contents remain skipped in non-XML mode.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-15T04:24:05.290187Z 38450f6 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

findAll and findOne now traverse child arrays depth-first. They save parent arrays and resume indices while descending, then resume the parent after exhausting a child array. Non-XML mode continues to skip <template> contents.

Changes: Query traversal

Layer / File(s) Summary
Depth-first traversal in query helpers
src/helpers/querying.ts
findAll and findOne replace breadth-first array queues with stacks of parent arrays and resume indices. Both switch to child arrays when descending and return to parent arrays after exhausting children.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Suggested reviewers: fb55

Merge Risk: Minimal · up to 38450

This change speeds up the query traversal helpers without altering the order or the results of matching. No merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: findAll and findOne in src/helpers/querying.ts switch from breadth-first queues to push/pop stacks for depth-first traversal. The title uses the conventional perf(...)…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: findAll and findOne in src/helpers/querying.ts switch from breadth-first queues to push/pop stacks for depth-first traversal. The title uses the conventional perf(...) prefix and is specific. The objectives confirm the same change, with unchanged visit order and results.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit hops through nested trees,
Finds each child with practiced ease.
It saves its place, then travels deep,
And climbs back up when branches sleep.
The query paths now trace their way,
Through leafy nodes from root to spray.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 6a3eb57f-0c36-4583-803c-bd6a3ad883b3
Commits

Reviewing files that changed from the base of the PR and between 84ec162 and 38450f6.

Files selected for processing: 1

  • src/helpers/querying.ts

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 1 file

View guided diff | Re-trigger cubic

@greptile-apps

greptile-apps Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] This PR appears safe to merge.

Summary

Reworks findAll and findOne to keep the current array and index in local variables.

  • Tree queries keep their visit order while using push and pop for child lists.

Reviews (1) · Last reviewed commit: "perf(querying): use push/pop stacks in f..." · Reviewed by Greptile

This branch has not been deployed

No deployments
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.

1 participant