killed 200 lines of JS with :has(), then Safari had opinions

kewldad

New Member
Messages
60
Reaction score
0
Location
Calgary, AB

Rebuilt our filter UI last week. Used to be a pile of JS toggling a .is-filtered-out class on every product card whenever you ticked a sidebar checkbox. Ripped all of it out for one nested :has() selector sitting on the grid container. Worked on the first try in Chrome. Felt like cheating, honestly.

Then QA got their hands on it. Safari 17 does support :has(), but the re-evaluation when you toggle a descendant is rough. I'm seeing 200ms+ of jank on a 60-item grid, every single checkbox click. Chrome doesn't blink. Firefox is fine. Safari just chugs like it's recomputing the whole tree.

So now I'm back to a hybrid mess: :has() for the initial paint, then a sprinkle of JS for the toggle path. Which is probably the worst of both worlds and my senior dev is gonna side-eye it in review.

Curious if anyone else has hit the re-eval cost. My selector was 4 levels deep, which I know is asking for trouble. Happy to be told I'm being an idiot here.

w0lfpack88

New Member
Messages
72
Reaction score
0
Location
Oslo, NO

Four levels deep is the killer imo. Safari's invalidation walks the whole subtree on any descendant mutation, so the deeper the :has() chain the more it has to re-check on every toggle. Chrome does the same thing in theory, it's just way more aggressive about caching the negative result.

What worked for me on a similar grid: move the :has() off the grid container and onto a wrapper that only covers the cards, and make sure the thing being toggled isn't a sibling of the grid. If the checkbox is outside the :has() scope, Safari seems to handle it a lot better. Also killed a bunch of :not(:has(...)) nesting I had going on, that stuff compounds.

Honestly the hybrid thing isn't as ugly as you think. Initial paint is where :has() shines, toggle path is where JS wins. I've got a project running exactly that split for like 8 months now and nobody's died. Senior dev side-eye is survivable.

Curious though, is your grid virtualized at all? 60 items shouldn't be 200ms territory even on Safari unless something else is forcing layout too.

ChrisM85

New Member
Messages
62
Reaction score
0
Location
Dublin, IE

Four levels is almost certainly the bulk of it. I had a 3-level :has() on a product grid last year and just flattening it to 2 cut the toggle time roughly in half on Safari. Not a benchmark, just me squinting at the performance panel, but the pattern was obvious.

On the virtualization question, no, it's a flat 60 items, nothing fancy. Which is why the 200ms bugs me so much. Turned out I had a transition on card height that Safari was re-resolving on every filter pass. Killed the transition, dropped to like 40ms. So yeah, w0lfpack88 might be right that something else was forcing layout on top of the invalidation cost. Worth checking if you've got any animated properties on the cards themselves.

Where I'll push back a little: the wrapper trick only half worked for me. My checkboxes already sit outside the grid scope and Safari still re-evaluated the whole thing on toggle. Maybe my markup was just cursed. And I'm with you on keeping the hybrid. Chasing a pure CSS solution that one engine punishes isn't worth it, and "initial paint in CSS, toggle path in JS" is easy enough to explain in review.

Unrelated but I burned an hour last week blaming Safari for stale styles when it was my bundler caching old CSS. Check your build output before you refactor anything, haha.

tommy2k

New Member
Messages
69
Reaction score
0
Location
Mexico City, MX

ChrisM85 you clipped your post mid sentence there lol, what were you blaming Safari for.

Anyway, mild pushback on the "hybrid is fine, ship it" consensus. It is fine until it isn't. The thing that bit me with that exact split was the two paths drifting apart. CSS handles initial paint, JS handles toggles, then three months later someone adds a new filter state in JS only and now the server rendered markup and the client rendered markup disagree for one frame. Got a nice little flicker on first interaction, every time. Took me an embarrassingly long time to figure out the CSS :has() was applying a rule the JS path didn't know about.

Not saying don't do it, I'd probably still do it. Just leave yourself a comment explaining which path owns which state, because future you will not remember.

w0lfpack88's thing about the scope is the part I'd actually test first though. If the checkbox lives outside the :has() subtree, Safari's invalidation should be way cheaper because it's not re-walking the thing that just changed. Worth a 20 minute experiment before you go rewriting the JS.

Also is this Safari 17 flat or a 17.x point release? I've seen :has() behave noticeably different between minor versions and I never trust a perf bug until I've checked it on a couple builds.

emilyb85

New Member
Messages
67
Reaction score
0
Location
Amsterdam, NL

tommy2k's drift point is the real one and I don't think a comment actually fixes it. A comment is just a note that the next person ignores. What saved me on a similar split was making the JS path read the same state the CSS does, like putting a data-filtered attribute on the card and having both the :has() rule and the toggle code key off that single source. Then if someone adds a new filter state, they have to touch the markup, and the CSS and JS both pick it up or neither does. Sounds obvious but it took me two drift bugs to get there.

ChrisM85 the transition thing is a good catch but I'd be surprised if that's the whole 200ms. Killing a transition gets you from "real bad" to "less bad," it doesn't usually account for the full hit unless the transition is on something expensive like height on a flex child, which, ok, fair, that's a layout bomb.

What I actually want to know is whether OP tried the scope experiment w0lfpack88 mentioned before going hybrid. Because if the checkbox is genuinely outside the subtree and it's still 200ms, then it's not the invalidation depth at all and everyone here is debugging the wrong layer.

ahmed_khan92

New Member
Messages
63
Reaction score
0
Location
Toronto, ON

emilyb85's data-filtered idea is clean in theory but it quietly reintroduces the thing OP was trying to delete. If the JS has to write an attribute onto all 60 cards on every toggle, you're back to touching the whole grid, just with a nicer name on it. The win there is maintainability, not perf. Worth saying out loud before someone sells it as the fix for the 200ms.

For the actual jank, the thing nobody's mentioned is contain. Slap contain: layout style on the cards and Safari stops treating that subtree as something it has to re-evaluate from the top on every invalidation. Did this on a filter grid last year and it was the difference between the perf panel looking like a staircase and looking flat. It's not a :has() fix, it's a "stop the engine from being paranoid" fix, and it stacks fine with whatever you do with the scope.

tommy2k asked the right question and then his post cut off, so I'll ask it again. OP, what Safari version exactly? I had :has() behave noticeably different on 17.0 vs 17.4, like not just slower, actually different invalidation behavior. If you're on a flat 17.0 then half this thread might be debugging a bug Apple already fixed.

da_real_mvp

New Member
Messages
69
Reaction score
0
Location
Atlanta, GA

contain: layout style saved me too, but heads up, it's not free. Had a card with a flyout menu that needed to escape the card bounds and containment ate the popover whole. Spent a whole afternoon blaming z-index before I remembered I'd added it three weeks earlier. Layout containment alone is fine for most grids, it's when you go full paint containment that the fun starts. Just flagging it since ahmed_khan92's advice is right, it's just not a no-op.

On the emilyb85 vs ahmed_khan92 thing, you're both arguing past each other. emily is talking about drift, ahmed is talking about perf, and neither of you is wrong, you're just answering different questions. The data-filtered attribute is a maintainability play and it's a good one, but ahmed's right that it doesn't touch the 200ms.

The thing I'd actually try, and have you tried this OP: get :has() out of the toggle path entirely. Write one attribute on the grid container, something like data-active-filters="brand:acme,size:xl", and key your card rules off that. :has() then only handles the server rendered initial state, which is the one place it's genuinely great. Toggle cost becomes a single attribute write no matter how many cards are in the grid. It's still the hybrid, but the seam lives in a place that's way harder to drift because both paths are reading the same attribute.

Last thing, how many :has() rules are actually stacked on that container? If it's one selector that's one invalidation pass. If you've got four or five separate :has() rules doing different filter groups, Safari is doing the walk four or five times per click and that compounds way faster than people expect. Collapsing five into one took me from 180ms to under 50 on a similar grid, no selector rewriting needed.

tallguy_tx

New Member
Messages
61
Reaction score
0
Location
Austin, TX

da_real_mvp's data-active-filters idea is basically where I landed, but for a different reason. I run a live odds grid (60-80 rows depending on the sport) and toggle jank was the least of my problems. The real killer was Safari on mobile re-evaluating on every price update tick, not just filter clicks. Same invalidation walk, just triggered by a mutation nobody asked for.

What actually flattened it for me was cutting the NUMBER of :has() rules, not the depth. Everyone here is talking depth, and yeah 4 levels is rough, but I had like six separate :has() selectors doing little one-off things on the same grid and that compounded way worse than one deep chain ever did. Collapsed them into a single rule keyed off a container attribute and Safari stopped chugging. Nobody ever mentions the rule count and I think it matters more than people realize.

contain: layout style helped too, but da_real_mvp is right that it bites you the second anything needs to escape the card. My row tooltips got eaten alive and I spent an embarrassing amount of time blaming my own z-index stack.

tommy2k's version question never got answered and I want to know too. I'm stuck on 17.2 because of an old test device and I'm half convinced I'm debugging a ghost Apple already killed.

da_real_mvp

New Member
Messages
69
Reaction score
0
Location
Atlanta, GA

tallguy_tx the rule count thing is the most useful post in this thread and I don't think the framing is quite right. It's not the number of :has() rules by itself, it's whether they share an invalidation root. I had four separate :has() selectors on a dashboard grid that all anchored off different wrapper elements, and collapsing them into one rule keyed off a container attribute did nothing for me. Absolutely zero change. Then I moved all four so they anchored off the same element and suddenly Safari calmed down. Same rule count as before in the middle version. So the count is a proxy for "how many independent subtrees is the engine re-checking", which is the actual cost.

Also, and I say this as someone who has burned days on it, the live odds case is a totally different beast from OP's. Price ticks mutating text nodes are going to invalidate regardless of how clever your selector is, and no amount of scope tuning fixes a grid that's re-laying out on a timer. That's a throttling problem dressed up as a CSS problem. How are you batching the ticks? rAF? Because if you're writing to the DOM on every socket message then :has() was never the villain there.

And tallguy_tx you clipped your post too, what's the old thing keeping you on 17.2? You can't just leave us hanging twice in one thread lol.

On the version question everyone keeps re-asking: I went looking for the 17.0 vs 17.4 invalidation difference people keep referencing and I couldn't reproduce it. Ran the same 3-level toggle test on both, numbers were within noise of each other. Not saying it's not real, maybe my test was too small, but I'd want someone to post an actual before/after before we all start pinning bugs on a point release.

You must log in or register to reply here.