Skip to content

perf: collapse per-child loop in findWithMatcher#566

Open
jvoisin wants to merge 1 commit into
PuerkitoBio:masterfrom
jvoisin:collmatch
Open

perf: collapse per-child loop in findWithMatcher#566
jvoisin wants to merge 1 commit into
PuerkitoBio:masterfrom
jvoisin:collmatch

Conversation

@jvoisin
Copy link
Copy Markdown
Contributor

@jvoisin jvoisin commented May 29, 2026

findWithMatcher iterated over each element child of every input node and called m.MatchAll(c) on each, then append-spread-merged the returned slices into one result. For a node with K element children this was K independent cascadia tree walks, each starting with a fresh nil result slice.

This commit calls m.MatchAll(n) once on the input node itself. cascadia walks n plus all descendants in document order, so if n matches it is always the first element of the returned slice, strip it to preserve jQuery Find's "descendants only" semantics.

Benchmarks (-count=20, benchtime=500ms, linux/arm64):

                      sec/op           B/op             allocs/op
Find                  ~ (-10%)         -23.53%          -7.69%
FindWithinSelection   ~                -11.06%          -43.53%
FindNodes             -12.36%          ~                ~
Has                   ~                -17.20%          -7.14%
Add                   ~                 -5.13%          -9.09%
Closest               -37.34%          ~                ~
ClosestSelection      -43.77%          ~                ~
ClosestNodes          -36.56%          ~                ~
MetalReviewExample    ~                 -5.42%         -11.19%

findWithMatcher iterated over each element child of every input node
and called m.MatchAll(c) on each, then append-spread-merged the
returned slices into one result. For a node with K element children
this was K independent cascadia tree walks, each starting with a
fresh nil result slice.

This commit calls m.MatchAll(n) once on the input node itself. cascadia walks n
plus all descendants in document order, so if n matches it is always the first
element of the returned slice, strip it to preserve jQuery Find's "descendants
only" semantics.

Benchmarks (-count=20, benchtime=500ms, linux/arm64):

                          sec/op           B/op             allocs/op
    Find                  ~ (-10%)         -23.53%          -7.69%
    FindWithinSelection   ~                -11.06%          -43.53%
    FindNodes             -12.36%          ~                ~
    Has                   ~                -17.20%          -7.14%
    Add                   ~                 -5.13%          -9.09%
    Closest               -37.34%          ~                ~
    ClosestSelection      -43.77%          ~                ~
    ClosestNodes          -36.56%          ~                ~
    MetalReviewExample    ~                 -5.42%         -11.19%
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