<script src='https://cdnjs.cloudflare.com/ajax/libs/classnames/2.3.2/index.min.js'></script>
<script src='https://unpkg.com/clsx@1.2.1/dist/clsx.min.js'></script>
var str = 'style';
var obj = {
'style-2': true,
'style-3': false,
'style-4': true,
}
var arr = ['style-5', 'style-6']
function composeCssClass(values) {
const classNameStack = []
for (let i=0; i<values.length; i+=1) {
const value = values[i]
switch (true) {
case !value: {
continue
}
case typeof value === "string":
case typeof value === "number": {
classNameStack.push(value)
continue
}
case Array.isArray(value): {
const valueFromArray = composeCssClass(value)
if (typeof valueFromArray !== 'undefined') {
classNameStack.push(valueFromArray)
}
continue
}
case typeof value === "object": {
for (const key in value) {
if (value[key]) {
classNameStack.push(key)
}
}
continue
}
}
}
return classNameStack.length > 0 ? classNameStack.join(" ") : undefined
}
let result = window.classNames(str, obj, arr, 'test classname')
let result = window.clsx(str, obj, arr, 'test classname')
let result = composeCssClass(str, obj, arr, 'test classname')
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
classnames | |
clsx | |
custom implemenatation |
Test name | Executions per second |
---|---|
classnames | 2705606.2 Ops/sec |
clsx | 3971421.2 Ops/sec |
custom implemenatation | 2093373.1 Ops/sec |
Let's break down what's being tested in the provided JSON.
Benchmark Definition
The benchmark is comparing three approaches to generate a CSS class string from an array of values:
classnames
: A library function that takes multiple arguments and returns a concatenated string of classes.clsx
: Another library function that serves a similar purpose, but with some differences in implementation.composeCssClass
) written by the benchmark creator.Options Compared
The three options are compared in terms of their performance (number of executions per second) across different browsers and devices.
Pros and Cons of Each Approach
classnames
:clsx
:classnames
, but with some optimizations and features (e.g., support for JSX-like syntax).classnames
.composeCssClass
):Library and Its Purpose
classnames
: A small library that provides a simple way to concatenate class names from multiple sources. It's designed to be easy to use and performant.clsx
: Another lightweight library that extends the capabilities of classnames
with additional features, such as support for JSX-like syntax and better performance in certain scenarios.Special JS Feature or Syntax
None mentioned in this benchmark.
Benchmark Preparation Code
The preparation code sets up the test case by defining:
str
obj
arr
These variables are then passed to each of the three functions being compared, which generate a CSS class string from them.
Other Alternatives
If you're interested in exploring alternative libraries or approaches for generating CSS class strings, some options include:
chain
methodcss-classify
or class-join
Keep in mind that each option has its pros and cons, and the choice ultimately depends on your specific use case, project requirements, and personal preference.