Closed lomegor closed 6 years ago
Hi @lomegor, thank you for the report. Selector engine tuning is complicated, as you've seen in the Sizzle codebase, and there are always tradeoffs. We currently process every complex selector right-to-left (i.e., establishing a candidate set and then checking for ancestors/preceding siblings that match earlier compound selectors), and then process the simple selectors of each compound selector right-to-left as well (on the assumption that the rightmost ones will be the most specific and therefore winnow out undesired elements at the earliest point).
In this case, that means testing :not(:has(a[data-quickitemmodal]))
for every ancestor of every a
element before checking whether that element is a div
or has class "gc-group-items". If you're using Sizzle through jQuery, we're working on making this better leverage native browser selector engines in a future release, and right now you can break apart your selector to force an efficient sequence (e.g., jQuery('div.gc-group-items').not(':has(a[data-quickitemmodal])').find('a')
). Or if you're using Sizzle directly, you can do something very similar via different means.
I was working with a complicated and slow selector on a site that is taking more than 7 seconds to return an empty set. The selector I was using is
Sizzle('div.gc-group-items:not(:has(a[data-quickitemmodal])) a')
on URL https://www.orientaltrading.com/halloween-costumes/mens-costumes-a1-555609-1.fltr?categoryFromSearch=true&rd=mens%20costumes&mpp=192The first interesting thing about it is that are no matching
div.gc-group-items
so I don't think anything else needs to be checked. I'm not sure how the internal of Sizzle work, so I may be wrong about that though.The second thing I noticed is that older versions of Sizzle seem to run this a lot faster, and I pinpointed the problem to the fixes done on https://github.com/jquery/sizzle/commit/b948c8a2414d6bcc60c1c3091a70647db54afa98, so I'm thinking that it may have something to do with the way that cache is stored. The issue is not happening on Sizzle 1.10.15, where the selector even on first runs takes less than 500ms. Reverting those changes improves the speed dramatically (although I understand this may reintroduce some bugs).
I tried my hand at fixing this, with no luck, as to be honest, I don't understand much of that code and what it's doing.
I understand that the selector is pretty non-standard and makes sense for it to be slow, but it shouldn't be reaching even one second I think.