Zero Runtime
styled-static achieves near-zero runtime overhead by generating components at build time. The entire runtime is just ~45 bytes minified.What’s “Zero Runtime”?
Zero runtime means:- No runtime stylesheet generation - CSS is extracted at build time
- No runtime parsing - No CSS parser shipped to browser
- No runtime interpolation - No dynamic
${props => ...}evaluation - Inline components - Components generated at build time, not runtime factories
- A minimal className merge helper (~45 bytes)
- No other runtime code
Runtime Size Comparison
| Library | Runtime Size (Minified) | Runtime Size (Brotli) |
|---|---|---|
| styled-static | 45 B | 50 B |
| Restyle | ~2.2 KB | ~900 B |
| Linaria | ~1.5 KB | ~700 B |
| Emotion | ~11 KB | ~4.5 KB |
| styled-components | ~13 KB | ~5 KB |
The Complete Runtime
Here’s the entire runtime code:/home/daytona/workspace/source/src/runtime/index.ts:1-28 for source.
That’s it. 1 function, 1 line of logic, ~45 bytes minified.
The m Function (Merge)
Purpose
Merges the styled component’s base className with any user-provided className.Signature
Behavior
Why Not Use Template Literals?
Build-Time vs Runtime
Build Time (Plugin)
All heavy lifting happens at build time:Runtime (Browser)
Only className merging happens at runtime:Generated Component Structure
Inline Component Pattern
The plugin generates components usingObject.assign:
- Inline function - No factory overhead, no closure allocation
- Static
.className- Enables manual composition - Single object - Component and metadata in one
- No React.memo needed - Already optimized
Component vs Factory Pattern
Traditional CSS-in-JS uses factories:| Aspect | Factory Pattern | Inline Pattern |
|---|---|---|
| Closure allocation | Yes | No |
| Factory overhead | ~100-500ms | 0ms |
| Memory per component | ~1-5 KB | ~100 bytes |
| Runtime dependencies | Large | Minimal (~45 B) |
Zero-Runtime Features
Some features have literally zero runtime cost because they’re completely replaced at build time:1. css Helper
2. keyframes
3. createGlobalStyle
4. withComponent
m function)
Variant Runtime Cost
Simple Variants: If/Else Chain
For variants with ≤4 values, the plugin generates if/else chains:- Zero allocation (no object/map creation)
- Fast for small variants (2-4 branches)
- Minifies well
Complex Variants: Hoisted Map
For variants with >4 values, the plugin generates a hoisted static map:- O(1) lookup (faster than long if/else chains)
- More compact code for many variants
- Static map is shared across all instances
/home/daytona/workspace/source/src/codegen.ts:82-223 for implementation.
cssVariants: Pure Functions
cssVariants generates pure functions that return className strings:
Compound Variants: Zero Runtime
Compound variants have zero runtime cost because they work through CSS specificity:- Individual variant classes are applied:
ss-btn--size-lg ss-btn--color-danger - Combined selector matches automatically:
.ss-btn--size-lg.ss-btn--color-danger - CSS cascade applies compound styles (higher specificity wins)
/home/daytona/workspace/source/src/vite.ts:441-452 for CSS generation.
React Integration
Using React’s createElement
The plugin importscreateElement from React:
- Plugin runs after React plugin (JSX already transformed)
createElementis smaller than JSX runtime- Direct function calls are faster
No forwardRef Needed
React 19+ automatically forwards refs:React.forwardRef wrapper (saves ~20 bytes per component).
Performance Characteristics
Component Creation
| Operation | styled-static | Traditional CSS-in-JS |
|---|---|---|
| Parse CSS | Build time | Runtime |
| Generate styles | Build time | Runtime |
| Inject stylesheet | Build time | Runtime |
| Component creation | 0ms (inline) | ~0.1-1ms (factory) |
Runtime Operations
| Operation | styled-static | Traditional CSS-in-JS |
|---|---|---|
| Render component | ~0.01ms | ~0.05-0.2ms |
| className merge | ~0.001ms | ~0.01ms |
| Style recalculation | 0ms (static CSS) | ~0.1-5ms (dynamic) |
Memory Usage
| Metric | styled-static | Traditional CSS-in-JS |
|---|---|---|
| Runtime bundle | ~45 B | ~5-15 KB |
| Per component | ~100 B | ~1-5 KB |
| Stylesheet cache | 0 B (static CSS) | ~10-100 KB |
Tree Shaking
All styled-static code is tree-shakeable:Minification
Generated code minifies extremely well:Next Steps
- Learn about How It Works - complete transformation pipeline
- Learn about Static Extraction - CSS extraction details