question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Slow first render (generateAndInjectStyles)

See original GitHub issue

Environment

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 time
  • generateAndInjectStyles (@styled-components.browser.esm.js:1443): 805.98ms total time - 0.29ms self time
  • inject (@styled-components.browser.esm.js:1080) - 795.61ms total time - 0.38ms self time
  • insertRules (@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:closed
  • Created 5 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
robertontiucommented, Nov 1, 2018

@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:

import * as React from 'react'
import styled, {css} from 'styled-components'

interface FixLagState {
	ready: boolean
}

const Wrapper = styled.div`
	display: none;
	${(props: FixLagState) => props.ready && css`
		opacity: 0;
	`} 
`

export default class FixLag extends React.Component<{}, FixLagState> {
	state = {
		ready: false
	}

	render() {
		return <Wrapper {...this.state}/>
	}

	componentDidMount() {
		this.setState({ready: true})
	}
}

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).

0reactions
robertontiucommented, Nov 6, 2018

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found