-
Notifications
You must be signed in to change notification settings - Fork 298
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
Proposal: add computed
attribute for great performance!
#1318
Comments
I feel like this could be lazy or helped via CSS similar way will-change helps but using will-compute or similar so that hints are obvious and could be optimized on the right side of affairs? 🤔 |
will-compute: width height scrollTop; It looks weird. Or use keywords will-compute: rect scroll;
There are many more computed values that need to be listed. |
mine was just an idea but I like |
Most browsers (all?) already do "store" the full computed box values and only recompute it when the layout has been "dirtied". So doing something like for (const elem of elems) {
const rect = elem.getBoundingClientRect();
// do something with rect
} is not in itself "bad". What's bad is when in the So this could actually be done in user-land by running a first loop that will gather all the needed values, and a second loop that will use these gathered values. This obviously is fragile as it requires no other code did dirty the layout before you gathered the values, but while having an out-of-date state accessible might be useful in some (rare?) cases, a lot of APIs and properties do need an up-to-date box-model and thus trigger reflows. I don't think a single property can hold all of these (e.g. how would you call Unfortunately there are so many different things that do trigger a reflow that a catch-all solution seems almost impossible. Personally, to cope with this issue I use a strategy which registers a couple of callbacks: one getting the computed values after a |
@Kaiido wow!
I had no idea even that static rectangle would trigger a reflow but thanks for the details and everything reasons well to me ... back to CSS wonderland, we have an easy way to write or read properties from JS but no way to be reactive ... would it be possible or make sense to have a way to hook into a CSS variable so that, when it changes, more can be done? I know this might feel like looking for troubles, same way resize triggering a resize would do, but I am thinking if |
I must admit I'm not entirely sure what exact use case you have in mind, but indeed most might certainly be handled on their own with alternative proposals. For instance I think |
What problem are you trying to solve?
When we need to frequently read some values from DOM Element, the browser will perform reflow, which is a very large performance consumption. For example, read scrollTop, getBoundingClientRect. I believe there are many scenarios that need to frequently read the changes of these values, but doing these now will affect performance.
Why not pre-store the value in Element when calculating the next frame inside the browser to avoid reflowing again when reading, so as to greatly improve performance?
If pre-storing the calculated value for each element will consume too many resources, then we can support a property to indicate that the specified element needs to pre-store the calculated value, which will greatly improve performance without wasting resources.
I temporarily think of using
computed
. If there is a better proposal, please discuss it.The text was updated successfully, but these errors were encountered: