4 css rules that make safari 35x faster
文章目录
部分视频或图片暂未收录,已在正文保留提示;需要时可查看原文。查看原文 ↗
lately we have been doing lots of performance work. when integrating scroll area and testing it one of my colleagues found a weird issue in safari. the issue was that large lists were taking much more time to scroll than they should. i decided to spend some tokens and figure out what this problem really was, to understand whose problem this is: safari's or base ui's, and what we can do about it. it turned out the problem is in safari and not base ui.
safari takes 106 ms on every frame when you scroll a list of 3,000 items. so, as i understand, that's around 9 fps. but if we add 4 css rules, this value drops to 3 ms. i will explain what these rules are below. but first, we need to understand what happens during scrolling itself.
what happens on every scroll
the fade itself is not built into base ui. what is built in is its logic for calculating the values, which you can already use on your side to render the fade. scroll area calculates how far you are from the edge where the fade should appear. all these values are pushed on scroll, even if you don't have a fade.
the problem is that custom properties inherit. and if we change a custom property on an element, all children inherit a new value for all these custom properties. so we need to recalculate styles for everything that goes in children. in my test i had 39 thousand elements, and it turned out that on every scroll event all these values had to be recalculated for all of these items.
why chrome is fine
css has an ability to say that these custom properties should not be inherited. you write it through inherits with the flag false, and changes on the parent then stay on the parent. base ui already does it, but with a nuance. it is enabled everywhere except the webkit engine, so everything works instantly in other engines, but not in safari.
but there is a reason why safari became an exception.
it is not hard to guess that this reason is a webkit bug. as soon as a property stops being inherited, a child that still needs this value can request it through the inherit keyword. in safari it resolves to a wrong value (bugs.webkit.org/show_bug.cgi?id=325900). so anyone using this value ends up with a broken fade.
the library can't know by default how much you actually need this logic, so obviously it doesn't make this decision for you, instead it chooses the lesser of two evils: a slower scroll in safari, but not the broken ui for you.
the fix
but you can fix it on your side if you don't need this inheritance behavior. you can add 4 properties in your css, and the scroll in safari will work as it should. the only caveat is that after this you can use these values only on the viewport itself. if you pass them to children through inherit, in safari it will break, so check your css first.
you should not worry about the duplication. chrome ignores duplicates, so the fact that we're duplicating the code that is already in base ui, nothing will break and everything will work the same if your users open the app in chrome.
will this be fixed?
probably yes. because webkit needs to fix the bug first (it will probably never happen lol). then base ui will be able to remove the check on their side, and everything will work by default for all people as it should, and you won't need to do anything.
created a gist prompt for your agent to fix this: https://gist.github.com/pqoqubbw/a5d168f571d0283269f2987a7de3728f
credit to my colleague for catching this in the first place.









