Performance Characteristics
What you'll learn: Why CSS Modules are a performance win—they compile to plain CSS with no JavaScript overhead at runtime.
Zero Runtime Cost
CSS Modules might look like JavaScript magic when you import them, but they have a secret: at runtime, they're just regular CSS. During your build process (with Vite, Webpack, etc.), the tooling transforms your CSS Module files into standard CSS files and generates unique class names. By the time your app runs in the browser, the JavaScript only contains a plain object mapping your original names to the generated class names—no extra libraries, no CSS-in-JS overhead.
Why This Matters
Compare this to runtime CSS-in-JS solutions that generate styles on the fly: CSS Modules avoid the cost of parsing, serializing, and injecting styles during component render. Your browser gets a normal .css file it can parse once and cache efficiently. The JavaScript bundle stays lean—just those class name mappings.
Think of it like pre-cooking a meal versus cooking it every time someone's hungry. CSS Modules pre-compile everything, so at dinner time (runtime), you just reheat (apply class names).
The Trade
The "cost" isn't at runtime—it's at build time. Your build tool does the work once, generating unique class names like Button_primary__a8c7f. But that's a one-time expense during development or deployment, not something your users pay for with every page load.
Performance Profile
- No JavaScript execution to generate styles
- Standard CSS parsing and caching by the browser
- Minimal bundle size increase (just class name objects)
- Excellent runtime performance, identical to hand-written CSS
Key Takeaway: CSS Modules deliver the developer experience of scoped styles with the runtime performance of plain CSS—the best of both worlds.