When a component styled with a runtime CSS-in-JS library renders, what work does the library do, and what does it cost against a static stylesheet?
answer
- more than looking up a class
- serialise, hash, insert once
- a cache keyed by the CSS string
- new rules make the browser recalculate styles
- insert before anyone reads layout
basics
~20 sOn render it evaluates the style template with current props and theme, serialises it to CSS, hashes it into a class name and inserts the rule once per new hash, costing library bytes, render-time CPU and extra style recalculation.
solid answer
~50 sEach styled element runs a small pipeline while it renders: resolve props and theme, evaluate the template or style object into a CSS string, hash it into a class name, and, if that class has not been seen, compile and insert the rule into a `<style>` element, through the CSSOM or as text. Caches keyed by the CSS string mean identical styles share one class and one rule. Against a static stylesheet, which is downloaded once, parsed once and cached by HTTP, the costs are the library's own JavaScript, CPU during rendering, and **style recalculation**: React's docs warn that runtime injection forces the browser to recalculate styles much more often. Timing matters too, so libraries insert rules in a dedicated insertion step that runs before layout effects, ensuring code that measures layout sees the new styles.
go deeper
Remember that a runtime library generates CSS while components render and keeps an engine in the bundle, unlike a stylesheet that is simply downloaded.
Walk through the pipeline: evaluate, serialise, hash, insert once, reuse by hash. Then name the three costs: bundle bytes, render-time CPU, and style recalculation when new rules arrive.
Show how you would find the cost in a profile, which writing patterns defeat the caches, and why rules must be inserted before any layout effect reads the page.
Weigh whether the flexibility of runtime styling still pays for its render and recalculation cost across your product's long lists and interactions, or whether extraction should become the default.
## The pipeline behind one styled element A **runtime CSS-in-JS library** keeps its styling engine in the page. Take the event cards on a concert-ticketing site, written with a library such as styled-components. styled-components 6.5.3 documents its own render flow, which is typical of the family: 1. **Resolve context** — merge the component's props, any `attrs`, and the theme. 2. **Flatten** — run every interpolation in the template, so `${p => p.$soldOut ? ...}` becomes plain text. 3. **Hash** — turn the resulting CSS string into a short hash and a generated class name. 4. **Compile** — parse the CSS, add vendor prefixes and scope it to that class. 5. **Insert** — add the rule to a stylesheet: in the browser through `CSSStyleSheet.insertRule` or as a text node in a `<style>` element, on the server into an in-memory list. 6. **Render** — create the element with the generated class name. Steps 3–5 run only for CSS the library has not seen before, because the class name is derived from the CSS itself. ## What the caches save Caching is what keeps this affordable: - **Same CSS, same class.** Two buttons whose computed styles are identical hash to one class, and the rule is inserted once. - **Unchanged re-renders.** styled-components 6.4.0 added memoisation so that a re-render whose props and theme have not changed skips style resolution entirely; its changelog reports parent re-renders about 3.3 times faster than 6.3.12. - **Seen-before CSS.** When props change but produce a CSS string already seen, a lookup keyed by that string skips hashing and compiling. How styles are written also matters. Emotion's best-practices guide recommends defining styles outside the component body, because such styles "are only serialized once, instead of on every render". ## The costs against a static stylesheet | Cost | Runtime library | Static stylesheet | |---|---|---| | JavaScript shipped | The engine, in the bundle | None for styling | | Work while rendering | Evaluate, hash, sometimes compile and insert | None | | Rule insertion | Rules arrive as components mount, each one invalidating styles | All rules parsed once, up front | | Caching across visits | Rules regenerated on every page load | HTTP-cached file | | Growth | One new class per distinct style string | Fixed size | React's documentation on `useInsertionEffect` gives two reasons it does not recommend runtime `<style>` injection: it **forces the browser to recalculate styles a lot more often**, and it **can be very slow if it happens at the wrong time** in React's lifecycle. The first, React says, is not solvable; the second is about timing. ## Why the insertion moment matters If a library inserted rules during rendering while React was processing a non-blocking update, the browser would recalculate styles on every frame of that render. If it inserted them in an ordinary layout effect, other layout effects might measure elements before the new rules existed and read stale sizes. React therefore gives library authors a dedicated insertion hook that runs before any layout effects fire, so the rules are in place before any component code reads layout. When exactly that hook runs, and what it forbids, is a separate topic; the point here is that a runtime library needs *some* slot before layout is read. ## Seeing the cost in a real app On the ticketing site, the cost shows up in a few recognisable places: - **The first visit**: the bundle includes the engine, and the first render generates every rule the page uses. - **Long lists**: a results page with hundreds of event cards repeats the pipeline per card; identical cards are cheap, distinct ones are not. - **Interactions**: a hover or selection state written as a prop interpolation produces new CSS strings, and the new rules add more style recalculation. A performance profile makes this visible as scripting time inside component renders and as style-recalculation entries that follow rule insertion. A static stylesheet shows neither, which is the core argument for build-time extraction. Before switching families, three habits recover much of the cost: 1. Define static styles outside component bodies so they are serialised once. 2. Move continuous values, such as positions or colours from data, into custom properties set inline, so the rule text stops changing. 3. Express hover, focus and similar states with CSS pseudo-classes inside the static rule instead of props that change the generated CSS.
- What exactly does the library skip on a cache hit?In styled-components 6.4.0 and later, a re-render with unchanged props and theme skips style resolution entirely and only rebuilds the element's props. When props change but produce CSS seen before, a lookup keyed by that CSS string skips hashing and name generation, and the rule is not compiled or inserted again.
- Does it matter whether styles are defined inside or outside the component?Yes. Emotion's best-practices guide recommends defining styles outside the component body: they are then serialised once instead of on every render, and nobody can accidentally pass a dynamic value through them. Styles created inline in the render function are rebuilt each time the component renders.
- Why not insert the rules in an ordinary layout effect?Layout effects of other components may measure elements first, so they would read sizes computed from stale styles. A dedicated insertion step that runs before every layout effect guarantees the rules exist before any of that code runs. Inserting during rendering instead can force a style recalculation on every frame of a non-blocking update.
- Runtime CSS-in-JS costs nothing once the first paint is done
- Every render inserts a fresh rule even when the styles are identical
- The only cost of a runtime library is its download size
- Inserting rules during render is harmless because the browser batches it
- A static stylesheet is re-parsed whenever a component re-renders