-
Notifications
You must be signed in to change notification settings - Fork 50.2k
Description
React version: 19.0.0, 19.1.0
Steps To Reproduce
- Render at least two precedence levels, for example 'a' and 'b' so that react can insert our stylesheets into the head rather than inline.
- Render lots of style elements into 'a' (your first precedence level, the important part is that react inserts our stylesheets into the head) and observe performance. Compare this to rendering without any precedence at all.
Link to code example:
Here's a codesandbox demonstrating the basic issue:
https://codesandbox.io/p/sandbox/react-style-perf-t7w9qr
Or a github repository, if you prefer:
https://github.com/RJWadley/react-style-perf
The current behavior
Seems like each style tag is handled independently with very little optimization - which is probably fine if you only load one or two stylesheets but doesn't work well for larger amounts of styling. In the attached example, you should also notice that every time we toggle our styles with precedence our performance gets worse and worse.
Here's a real example of render performance on my M1 mac pro. This isn't from the attached example, but is the same issue just at a larger scale:
Zooming in, most of the performance hit seems to come from query selectors and from actually inserting each stylesheet.

The expected behavior
Stylesheets should ideally render in similar time regardless of precedence.
Related: #30739