What causes Recalculate Style in Chrome and what could be done to reduce the time for this step?
In an application with many elements (variable, e.g. 10,000) I observe Recalculate Style taking a long time when adding a class on the parent element of those elements. There are selectors that will affect elements of each subtree when the container has this class.
In the developer tools I was able to trace the cause of the Recalculate Style event by clicking here (screenshot of a MCVE):
From to the name I assume that this step involves calculating the effective (final) element style. I think this happens when
a changed (pseudo-) class
a changed CSS selector (importing new CSS, generating <style>
)
forces the browser to recalculate the CSS attributes of an element.
I created a MCVE with the same amount of elements as static HTML and toggling a class .change
on a .container
using a click
handler on the document - dead simple code.
The MCVE performs much better than the actual application, the step Recalculate Style takes less time. This is probably due to the simpler tree and less styles.
Then I started adding more styles to all selectors and the average time increased with every bunch of new CSS attributes. Adding more elements to the 10,000 subtrees did not change the average time.
So, I'd say that the amount of CSS attributes, the count of selectors affected and the count of elements affected influences this time.
Recalculate Style
Basically anytime you change a classname or other operations like that.
References
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With