Runtime CSS-in-JS deserves a rehearing
Runtime CSS-in-JS has largely been written off as too expensive for the browser. The reasons are familiar: some libraries rebuilt styles on every React render, interleaved stylesheet writes with rendering, accumulated generated CSS, and added React overhead to every styled element. Server rendering was awkward, React Server Components seemed incompatible, and build-time CSS appeared to eliminate…
Runtime CSS-in-JS has often been dismissed as too costly for browser performance. Libraries like React's CSS-in-JS rebuild styles on every render, intermix stylesheet writes with rendering, and generate excessive CSS. Server rendering, React Server Components, and build-time CSS were seen as problematic. In 2019, Aggelos Arvanitakis highlighted the hidden costs of CSS-in-JS libraries.
Two years later, Emotion maintainer Sam Magura reported that replacing Emotion with Sass Modules cut render times significantly. However, these measurements did not fully address the broader questions of whether every runtime-capable styling system incurs these costs and whether the flexibility gained justifies the expense.
Other companies have migrated away from runtime CSS-in-JS libraries in favor of alternatives that generate styles early and explicitly. GitHub moved from styled-components to CSS Modules to reduce server and client costs, while Linear adopted StyleX for performance and better component boundaries. Atlassian implemented Compiled to minimize runtime overhead and support concurrent rendering and streaming SSR.
The reasoning behind this shift is architectural. Style engines can perform work at different stages, and the benefits of a runtime-capable system extend beyond just speed. The key question is not whether runtime CSS-in-JS is fast or slow, but which decisions can be made earlier, and whether the flexibility offered justifies the cost. CSS does not always need to be generated in the browser, but having that option available is crucial when the final composition remains open.
To illustrate this point, the Tasty library, which the author created and maintains, serves as a case study. Tasty is a demanding test: it parses values, resolves states and conditions, composes extensions, generates and injects CSS in the browser. However, the same components can perform these tasks on the server or during a static build.
The author argues that a runtime-capable system can justify its cost when it controls when and how often that cost is paid and solves composition problems that matter to the product. This does not mean every CSS-in-JS library is fast or that browser generation is free. The decision to generate CSS at runtime should be made based on the specific needs of the product, particularly when some part of the styling structure remains unknown until runtime.
This open-ended approach allows for flexibility, enabling components to adapt to new combinations that were not predefined during development.
Rewritten by urgent.news; may contain errors — check the original.