Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slow performance with ":not(:has())" #428

Closed
lomegor opened this Issue Oct 15, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@lomegor
Copy link

lomegor commented Oct 15, 2018

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=192

The 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 b948c8a, 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.

@gibson042

This comment has been minimized.

Copy link
Member

gibson042 commented Oct 15, 2018

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.

@gibson042 gibson042 closed this Oct 15, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.