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.

hydration is slower than React when there are a lot of contexts

See original GitHub issue

Issue

When using a css-in-js library like emotion of framework like theme-ui (which uses emotion) and with enough components, preact hydration takes longer than react. This increases the first input delay metric for the site when there are a lot of styled components.

My theory is due to the amount of contexts in the app as emotion wraps each styled component in a context so that it has access to the theme. (When removing the contexts hydration times are comparable)

Reproduction

https://github.com/kenny-f/preact-hydration-repro

npm run build npm run start:prod

Go to localhost:3000

To run the app in `react

Steps to reproduce

Run the performance profiler in chrome devtools (Start profiling and reload page)

Here are some results:

Preact: preact-1

you can see from the gif below that the process is extremely deep: preact

React: react-2

I’m not sure if this a known issue as I couldn’t find any information on it. Just our own observations when profiling our app that does not have much functionality right now.

Happy to provide more information if required.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
jeremy-colemancommented, Jun 9, 2020

@developit i should have thought about this sooner, but the material ui repo has a benchmark setup for ssr, which could be useful to test this. I added to their setup to test several implementations of ‘styled’ libraries, and i was supprised to see styled-jss (the standalone version using jss v9) was by far the fastest. 2x faster than emotion and 5x faster than muis version. Emotion was ~50k op/s whereas styled-jss was close to 100k. Most others, including mui, were around 20k. I think this is likely due to using theme context, whereas styled jss only optionally uses the brcast based wrapper and doesnt wrap every component

2reactions
kenny-fcommented, May 12, 2020

@developit thanks for the response. Based on your reply I’ve done some more experiments.

First is dropping theme-ui and only use emotion directly: https://github.com/kenny-f/preact-hydration-repro/tree/emotion-only

preact: preact-emotion-only

react: react-emotion-only

as you can see the timings for both have dropped significantly.

The second is dropping both theme-ui and emotion and manually creating a ThemeContext and wrapping each div in a Consumer https://github.com/kenny-f/preact-hydration-repro/tree/manual-context

preact: preact-manual

react: react-manual

The timings drop even further here. This does seem to suggest that your theory is correct in that theme-ui and emotion maybe the culprits

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why Efficient Hydration in JavaScript Frameworks is so ...
But there's one catch. Most JavaScript frameworks need to hydrate top-down. This is as true for React as it is for Svelte. So...
Read more >
Progressive Hydration: For When React is Slow
Progressive hydration exacerbates this further, because React is very aggressive about removing content it does not recognize during hydration, ...
Read more >
Hydration is Pure Overhead - Builder.io
The hard part of hydration is knowing WHAT event handlers we need and WHERE they need to be attached. WHAT : The event...
Read more >
A tale of webpage speed, or throwing away React
None of the solutions is truly satisfactory for the author's use case for a single very strong reason: React's hydration process is both ......
Read more >
Water, Hydration and Health - PMC - NCBI - NIH
Nevertheless, there are major gaps in knowledge related to measurement of total fluid intake, hydration status at the population level, and few longer-term ......
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