Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Classnames vs CLSX vs Custom implementations
(version: 0)
Compare CLSX vs Classnames vs an simpliest implementation of creating a template string
Comparing performance of:
classnames vs clsx vs Entries, filter, map, join vs Custom clsx
Created:
2 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<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 Preparation code:
var c = { a: true, b: false, c: undefined, d: "lorem-ipsum" };
Tests:
classnames
let result = window.classNames(c)
clsx
let result = window.clsx(c)
Entries, filter, map, join
function cx(...input) { return input.filter(Boolean).join(" "); } let result = cx(c)
Custom clsx
const clsx = (classes) => { const sources = [] if (classes instanceof Array) { sources.push(...classes.filter(Boolean)) } else if (typeof classes === 'object') { sources.push(...Object.entries(classes || {}).filter(([, v]) => !!v).map(([c]) => c)) } else if (classes !== false) { sources.push(classes) } return sources.join(' ') } let result = clsx(c)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
classnames
clsx
Entries, filter, map, join
Custom clsx
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
classnames
5842298.0 Ops/sec
clsx
7289236.5 Ops/sec
Entries, filter, map, join
6399130.0 Ops/sec
Custom clsx
3965052.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark is designed to compare three approaches for creating a template string with class names: 1. `classnames` 2. `clsx` (short for "Class Names") 3. A custom implementation using basic JavaScript operations (filter, map, join) **Library Descriptions and Their Purposes** * `classnames`: This library provides a simple way to compose multiple class names into one string. It's designed to be fast and efficient. * `clsx` (also known as "Class Names"): Similar to `classnames`, but with some additional features like support for nullability and a more flexible API. * The custom implementation uses basic JavaScript operations (`filter`, `map`, and `join`) without relying on any external libraries. **Test Cases** Each test case executes one of the above approaches, passing an object `c` as input. Here's what each approach does: 1. `classnames`: Uses the `classNames` library to create a template string from the input object. 2. `clsx`: Uses the `clsx` library to create a template string from the input object. 3. "Entries, filter, map, join": Executes the custom implementation using basic JavaScript operations (`filter`, `map`, and `join`) on the input object. **Comparison of Approaches** Here's a brief comparison of the three approaches: * **Speed**: The custom implementation is likely to be the slowest due to its reliance on basic JavaScript operations. `classnames` and `clsx` are optimized for performance, making them faster than the custom implementation. * **Code Complexity**: The custom implementation has the highest code complexity due to its use of multiple JavaScript functions. `classnames` and `clsx` have simpler APIs with fewer dependencies. * **Flexibility**: Both `classnames` and `clsx` offer more flexibility in terms of handling edge cases, nullability, and other advanced features. **Other Alternatives** If you're interested in alternatives to these libraries, here are a few options: * `classnames`: `lodash.classnames`, which is a more comprehensive version of the library with additional features. * `clsx`: You could consider using `js-cookie` (which includes class name functionality) or creating your own custom implementation. **Considerations** When choosing an approach for this task, consider the following factors: * Performance: If speed is critical, use one of the optimized libraries (`classnames` or `clsx`). * Code Complexity: If simplicity is important, use one of the optimized libraries. * Flexibility: If you need to handle edge cases or advanced features, choose one of the optimized libraries. Keep in mind that this benchmark is focused on creating a template string with class names. Depending on your specific requirements, other approaches might be more suitable.
Related benchmarks:
lodash isNil vs native isNil
lodash isNil vs native js
lodash isUndefined vs typeof === undefined (take 2)
lodash isNil vs ! Operator
lodash isNil vs === null || === undefined
Comments
Confirm delete:
Do you really want to delete benchmark?