Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test_classnames_alt
(version: 0)
Comparing performance of:
classnames 2.3.1 vs keys, filter, join vs string based
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/classnames/2.3.1/index.min.js" integrity="sha512-6Wf/IjsSjLaFTYco3pXM+49kC5M7jtbHzxMcdmYvwDskjv7cMcBPmJX2053aoQ+LRi8Po4ZsCtkNMf+NhXhNyQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
Script Preparation code:
var obj = { 'style-2': true, 'style-3': false, 'style-4': true, }
Tests:
classnames 2.3.1
let result = window.classNames(obj)
keys, filter, join
function classes(obj = {}) { return Object.keys(obj) .filter((key) => obj[key]) .join(' '); } let result = classes(obj)
string based
function ts(...args){ let template = ''; args.forEach(arg => { if (typeof arg === 'string') { template += ' '+arg; return; } if (arg.join) { arg.forEach((e) => { template += ' ' + e; }) return; } let entries = Object.entries(arg); if (entries.length) { entries.forEach(entry => { if(entry[1]) template += ' ' + entry[0] }) } }) return template; } let result = ts(obj)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
classnames 2.3.1
keys, filter, join
string based
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmarking data and explain what's being tested. **Benchmark Definition JSON** The provided JSON defines two benchmarks: `test_classnames_alt` and three individual test cases. The main benchmark, `test_classnames_alt`, uses a JavaScript object (`obj`) to test the performance of different class name utilities. **Individual Test Cases** Each test case measures the performance of a specific approach to generating class names: 1. **classnames 2.3.1**: This test case uses the `classnames` library, which is a utility for working with CSS class names. The script includes the `classnames` library and passes an object (`obj`) as an argument. * Pros: Uses a well-established library that provides a standardized way of handling class names, making it easier to compare performance across different implementations. * Cons: May introduce additional overhead due to the inclusion of the library and its dependencies. 2. **keys, filter, join**: This test case uses a custom implementation of a class name generator. The script defines a `classes` function that takes an object as input and returns a string containing the class names. * Pros: Custom implementation can be optimized for specific use cases or performance requirements. * Cons: Requires manual management of key-value pairs, filtering, and concatenation, which may lead to slower performance compared to using a library like `classnames`. 3. **string based**: This test case uses a custom implementation that manually generates class names by iterating over the object's keys and values. The script defines a `ts` function that takes an array of arguments (potentially representing objects) and returns a string containing the combined class names. * Pros: Custom implementation can be optimized for specific use cases or performance requirements, and it avoids using external libraries. * Cons: Requires manual management of key-value pairs, filtering, and concatenation, which may lead to slower performance compared to using a library like `classnames`. **Library Explanation** The `classnames` library is a popular utility for working with CSS class names. It provides a simple way to test for the presence of specific classes in an object and return the corresponding value. **Special JS Feature or Syntax** None of the provided benchmark tests use special JavaScript features or syntax, such as async/await, ES6 modules, or modern web APIs. **Benchmark Preparation Code** The `Script Preparation Code` section defines a JavaScript object (`obj`) with three key-value pairs: `'style-2': true`, `'style-3': false`, and `'style-4': true`. This object will be used as input for the class name utilities being tested. The `Html Preparation Code` section includes a script tag that loads the `classnames` library version 2.3.1 from a CDN. **Alternatives** Other alternatives to using a custom implementation or the `classnames` library include: * Using other CSS class name libraries, such as `classnames-legacy` or `css-class-name`. * Implementing a custom class name generator using only vanilla JavaScript. * Utilizing a different approach, such as using a template engine (e.g., Handlebars) to generate class names. Keep in mind that the choice of implementation depends on specific requirements, performance constraints, and personal preference.
Related benchmarks:
luxon vs datefns vs moment vs dayjs (parsing)
Deep clone comparison
another classnames vs clsx
Classnames vs CLSX vs classNamesCurrent
Comments
Confirm delete:
Do you really want to delete benchmark?