<script src='https://cdnjs.cloudflare.com/ajax/libs/classnames/2.2.6/index.min.js'></script>
<script src='https://unpkg.com/clsx@1.1.0/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']
let result = window.classNames(str, obj, arr, 'test classname')
let result = window.clsx(str, obj, arr, 'test classname')
function ts(args){
let template = [];
args.forEach(arg => {
if (typeof arg === 'string') {
template.push(arg);
return;
}
if (arg.join) {
template = [template, arg];
return;
}
let entries = Object.entries(arg);
if (entries.length) {
entries.forEach(entry => {
if(entry[1]) template.push(entry[0])
})
}
})
return template.join(' ');
}
let result = ts(str, obj, arr, 'test classname')
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(str, obj, arr, 'test classname')
function ts(args){
let template = '';
args.forEach(arg => {
if (typeof arg === 'string') {
template += ' '+arg;
} else if (arg.join) {
arg.forEach((e) => {
template += ' ' + e;
})
} else {
let entries = Object.entries(arg);
if (entries.length) {
entries.forEach(entry => {
if(entry[1]) template += ' ' + entry[0]
})
}
}
})
return template;
}
let result = ts(str, obj, arr, 'test classname')
let objStyle = Object.entries(obj).filter((style)=>style[1]).map(style => style[0]).join(' ');
let arrComb = arr.join(' ')
let result = `${str} ${objStyle} ${arrComb} test classname`
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
classnames | |
clsx | |
Array based | |
String based (return struct) | |
String based (if-else struct) | |
Entries, filter, map, join |
Test name | Executions per second |
---|---|
classnames | 5753146.0 Ops/sec |
clsx | 12319609.0 Ops/sec |
Array based | 6206209.5 Ops/sec |
String based (return struct) | 11080346.0 Ops/sec |
String based (if-else struct) | 11066641.0 Ops/sec |
Entries, filter, map, join | 8429716.0 Ops/sec |
I'll break down the provided benchmark and explain what's being tested, compared, and their pros and cons.
Benchmark Overview
The benchmark compares four approaches to create a template string from an object with various types of values:
classnames
clsx
What's being tested
The benchmark is testing the performance of each approach to create a template string. The input data includes an object with strings, booleans, and arrays.
Options Compared
Here's a summary of the options compared:
classnames
vs clsx
: Both are popular libraries for creating class names dynamically.join
method.Benchmarks
The latest benchmark results show that:
clsx
library is slower than both own implementations using Array-based and String-based (if-else struct) approaches.Keep in mind that these results may vary depending on the specific use case, input data, and platform being used.
Conclusion
In conclusion, the benchmark highlights the importance of choosing an efficient approach to create template strings. The String-based (return struct) approach seems to be the fastest, followed closely by the Entries, filter, map, and join approach. Both own implementations using Array-based and clsx
library are slower but still viable options depending on specific requirements.