<script src='https://cdnjs.cloudflare.com/ajax/libs/classnames/2.2.6/index.min.js'></script>
<script src='https://unpkg.com/clsx@1.1.1/dist/clsx.min.js'></script>
<script src="https://cdn.jsdelivr.net/npm/@emotion/css@11.13.4/dist/emotion-css.umd.min.js"></script>
var c = { a: true, b: false, c: undefined, d: "lorem-ipsum" };
let result = window.classNames(c)
let result = window.clsx(c)
let result = window.emotion.cx(c)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
classnames | |
clsx | |
cx |
Test name | Executions per second |
---|---|
classnames | 5751470.5 Ops/sec |
clsx | 15618939.0 Ops/sec |
cx | 2886180.2 Ops/sec |
The benchmark represented in the provided JSON is designed to compare the performance of three different approaches to generating CSS class names in JavaScript: the classnames
library, the clsx
library, and the cx
function from the @emotion/css
library. It evaluates how quickly each method can generate class names from a given object that specifies which classes should be applied based on truthy values.
classnames (library):
let result = window.classNames(c)
classnames
library is a widely used utility for conditionally joining class names together. It allows developers to specify a variety of inputs (strings, booleans, objects) to determine which class names to include based on their truthy value.clsx (library):
let result = window.clsx(c)
clsx
is a small, fast utility for constructing className strings conditionally. It effectively combines class names and does so with a focus on performance and minimal footprint.classnames
, might not handle complex inputs as flexibly.cx (from @emotion/css library):
let result = window.emotion.cx(c)
cx
function from the Emotion library is primarily used for CSS-in-JS styling. It also functions as a utility to conditionally apply class names.The results show how many executions per second each method could perform on the specified browser environment. The results in this case are as follows:
clsx
: 7,338,414.5 executions per secondclassnames
: 5,848,881.5 executions per secondcx
: 4,460,173.0 executions per secondPerformance: The clsx
library outperforms both the classnames
and cx
implementations in this benchmark. If performance is your primary concern and you’re handling a large number of class name manipulations, clsx
may be the best choice.
Bundle Size: If minimizing the size of your JavaScript bundle is critical, again, clsx
is likely the preferable option due to its lightweight nature.
Functional Needs: If your use case requires extensive class name logic or you prefer a battle-tested solution with a larger feature set, classnames
might still be ideal despite its overhead.
Integration: If your project predominantly uses Emotion or needs CSS-in-JS support, then cx
is suitable, especially since it can leverage the underlying styling capabilities of Emotion.
Other alternatives to consider for constructing class names in JavaScript might include:
Vanilla JavaScript: Manually building class strings using template literals or string concatenation. This approach can yield the best performance but lacks the convenience of the libraries, especially when handling conditionals.
Tailwind CSS: For projects using a utility-first CSS framework, class management might be handled differently, focusing on predefined utility classes that don’t require dynamic generation.
When choosing between these options, consider trade-offs in size, complexity, and functional requirements. Each of these tools has its niche, and the best choice will depend largely on the specific needs of your project.