Slow first render (generateAndInjectStyles)
See original GitHub issueEnvironment
System:
- OS: Windows 10
- CPU: x64 Intel® Core™ i5-7200U CPU @ 2.50GHz
- Memory: 1.13 GB / 7.86 GB
Binaries:
- Yarn: 1.7.0 - ~\AppData\Roaming\npm\yarn.CMD
- npm: 6.0.0 - C:\Program Files\nodejs\npm.CMD
- Watchman: 4.9.4 - C:\Tools\watchman\watchman.EXE
npmPackages
- babel-plugin-styled-components: 1.7.1
- styled-components: 4.0.0
Reproduction
The project consists of multiple React root nodes that get rendered server side then hydrated on the client side. For each of the root node, a style tag and markup is outputed on the server side.
After the page is loaded and hydrated on the client, the very first interaction with a react component is very slow (~1s). Further analysis indicates that the function generateAndInjectStyles
is the one causing the issue. Here is a brief set of results from the performance analysis:
generateAndInjectStyles
(@styled-components.browser.esm.js:2025): 805.98ms total time - 0 self timegenerateAndInjectStyles
(@styled-components.browser.esm.js:1443): 805.98ms total time - 0.29ms self timeinject
(@styled-components.browser.esm.js:1080) - 795.61ms total time - 0.38ms self timeinsertRules
(@styled-components.browser.esm.js:812) - 795.23ms total time - 0ms self time
Expected Behavior
Either have this run more smoothly or have an option to run the style injection async on DOM ready (or manually).
Issue Analytics
- State:
- Created 5 years ago
- Comments:12 (6 by maintainers)
Top Results From Across the Web
React Styled Components performance - Stack Overflow
Nope. Styled Components, as a CSS-in-JS solution, is slow because the JS must be parsed before the CSS is generated, and only then...
Read more >Fix the slow render before you fix the re-render - Kent C. Dodds
There's something that your code is doing during the render phase that's making things slow. You should diagnose and fix that first.
Read more >How to Detect Slow Renders in React? - Alex Sidorenko
Profile the problem. Open React Developer Tools Profiler tab. Click the record button to start profiling. Interact with the part of your app ......
Read more >Free Automated Malware Analysis Service - powered by Falcon ...
https://navyfederalentry.typeform.com/to/XE0VZ2. This report is generated from a file or URL submitted to this webservice on February 24th 2019 00:13:42 ...
Read more >Transaction ... - Galileo POA Explorer
_getBuffer(e),n}},{key:"first",value:function(){return this.head.data}} ... color-profile color-rendering dominant-baseline enable-background fill-opacity ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@probablyup I did some extra profiling on the matter.
First of all, having one root node doesn’t fix the issue. The function (
generateAndInjectStyles
) still gets called on first re-render and takes about 1s. Further calls to this function are very fast. I assume styled-components attempts to do this for all the React tree the first time the reason for it being so slow.Secondly, I have actually found a temporary workaround:
Apparently having this component rendered triggers styled-components in the backround temporarily fixing our issue. It also appears the function takes a lot less since it only has to deal with the styles of this component (being rendered first in the DOM).
@probablyup Hey. The repo is private so we cannot share that. Also, the codebase is quite complex so we can’t make a env sample either.