JedWatson / classnames

A simple javascript utility for conditionally joining classNames together
MIT License
17.61k stars 562 forks source link

refactor: directly return newClass on falsy value #367

Closed noscripter closed 10 months ago

noscripter commented 10 months ago
Benchmarks ``` classnames git:(refactor) npm run bench > classnames@2.5.1 bench > node ./benchmarks/run.js (node:95772) ExperimentalWarning: Importing JSON modules is an experimental feature and might change at any time (Use `node --trace-warnings ...` to show where the warning was created) Benchmarking 'strings'. | Task Name | ops/sec | Average Time (ns) | Margin | Samples | | ------------- | ---------- | ------------------ | ------ | ------- | | default/local | 12,902,063 | 77.50698016049525 | ±0.94% | 6451032 | | default/npm | 15,087,679 | 66.27924267843498 | ±0.54% | 7543841 | | bind/local | 12,459,259 | 80.26158996638294 | ±0.81% | 6229630 | | bind/npm | 13,227,651 | 75.59920808713574 | ±2.60% | 6613827 | | dedupe/local | 4,725,515 | 211.61713059370376 | ±1.50% | 2362758 | | dedupe/npm | 3,741,350 | 267.28317406534336 | ±1.67% | 1870676 | Benchmarking 'object'. | Task Name | ops/sec | Average Time (ns) | Margin | Samples | | ------------- | ---------- | ----------------- | ------ | ------- | | default/local | 15,597,085 | 64.11454419143709 | ±1.31% | 7798543 | | default/npm | 15,509,932 | 64.47481349973197 | ±0.72% | 7754967 | | bind/local | 12,698,498 | 78.74946588230175 | ±1.03% | 6349250 | | bind/npm | 14,696,484 | 68.04348418880458 | ±1.47% | 7348243 | | dedupe/local | 9,326,803 | 107.2178640931543 | ±2.39% | 4663402 | | dedupe/npm | 6,130,380 | 163.1220093357168 | ±8.92% | 3065191 | Benchmarking 'strings, object'. | Task Name | ops/sec | Average Time (ns) | Margin | Samples | | ------------- | ---------- | ------------------ | ------ | ------- | | default/local | 12,227,576 | 81.78235224309321 | ±0.54% | 6113789 | | default/npm | 12,169,374 | 82.17349074400097 | ±0.83% | 6084688 | | bind/local | 10,086,583 | 99.1416002363269 | ±1.08% | 5043292 | | bind/npm | 11,379,709 | 87.87570587262846 | ±1.35% | 5689855 | | dedupe/local | 5,210,441 | 191.92232548319953 | ±0.43% | 2605221 | | dedupe/npm | 3,718,235 | 268.94478723254747 | ±2.45% | 1859118 | Benchmarking 'mix'. | Task Name | ops/sec | Average Time (ns) | Margin | Samples | | ------------- | --------- | ------------------ | ------- | ------- | | default/local | 6,273,738 | 159.39459539429376 | ±10.83% | 3136870 | | default/npm | 7,174,549 | 139.3815734381775 | ±0.51% | 3587275 | | bind/local | 5,776,720 | 173.10859353390987 | ±0.58% | 2888361 | | bind/npm | 6,528,800 | 153.16749138869034 | ±0.77% | 3264401 | | dedupe/local | 2,047,216 | 488.468106880368 | ±1.20% | 1023609 | | dedupe/npm | 1,789,916 | 558.6852887845175 | ±1.15% | 894959 | Benchmarking 'arrays'. | Task Name | ops/sec | Average Time (ns) | Margin | Samples | | ------------- | --------- | ------------------ | ------ | ------- | | default/local | 4,772,370 | 209.53945889482438 | ±0.84% | 2386186 | | default/npm | 4,988,038 | 200.47960598770234 | ±0.53% | 2494020 | | bind/local | 3,764,922 | 265.60971763127566 | ±0.62% | 1882462 | | bind/npm | 4,576,970 | 218.48513050245984 | ±0.78% | 2288486 | | dedupe/local | 2,132,913 | 468.84235318509957 | ±0.65% | 1066457 | | dedupe/npm | 1,870,374 | 534.6521433531376 | ±0.74% | 935188 | Finished! ```
noscripter commented 10 months ago

Benchmark result of the ternary operator change commit:

Benchmarks ``` classnames git:(refactor) ✗ npm run bench > classnames@2.5.1 bench > node ./benchmarks/run.js (node:7980) ExperimentalWarning: Importing JSON modules is an experimental feature and might change at any time (Use `node --trace-warnings ...` to show where the warning was created) Benchmarking 'strings'. | Task Name | ops/sec | Average Time (ns) | Margin | Samples | | ------------- | ---------- | ------------------ | ------ | ------- | | default/local | 13,444,968 | 74.37726608703976 | ±0.68% | 6722485 | | default/npm | 15,250,434 | 65.57190096307797 | ±0.66% | 7625218 | | bind/local | 12,650,225 | 79.04997733312605 | ±1.02% | 6325113 | | bind/npm | 14,403,641 | 69.42688651088662 | ±0.66% | 7201821 | | dedupe/local | 4,869,261 | 205.36995508964827 | ±1.32% | 2435849 | | dedupe/npm | 3,788,215 | 263.9765680740928 | ±1.53% | 1894108 | Benchmarking 'object'. | Task Name | ops/sec | Average Time (ns) | Margin | Samples | | ------------- | ---------- | ------------------ | ------ | ------- | | default/local | 15,856,490 | 63.06565589587619 | ±1.31% | 7928246 | | default/npm | 16,261,896 | 61.49344295704977 | ±0.63% | 8130949 | | bind/local | 12,859,256 | 77.76499421488091 | ±1.00% | 6429629 | | bind/npm | 14,954,401 | 66.86994495029627 | ±1.36% | 7477201 | | dedupe/local | 9,401,298 | 106.36828224239991 | ±1.86% | 4700650 | | dedupe/npm | 6,405,675 | 156.1115530739688 | ±2.47% | 3202838 | Benchmarking 'strings, object'. | Task Name | ops/sec | Average Time (ns) | Margin | Samples | | ------------- | ---------- | ------------------ | ------ | ------- | | default/local | 12,379,771 | 80.77693427684096 | ±0.55% | 6189886 | | default/npm | 12,258,657 | 81.57500124434846 | ±0.86% | 6129329 | | bind/local | 10,092,213 | 99.08629388691313 | ±1.02% | 5046107 | | bind/npm | 11,549,470 | 86.58405299610749 | ±1.30% | 5774736 | | dedupe/local | 5,177,388 | 193.14758445676514 | ±0.38% | 2588695 | | dedupe/npm | 3,747,376 | 266.85332855634147 | ±2.39% | 1873689 | Benchmarking 'mix'. | Task Name | ops/sec | Average Time (ns) | Margin | Samples | | ------------- | --------- | ------------------ | ------ | ------- | | default/local | 6,762,182 | 147.88124254788443 | ±1.79% | 3381092 | | default/npm | 7,259,784 | 137.74513518264786 | ±0.45% | 3629893 | | bind/local | 5,781,082 | 172.97798798829405 | ±0.58% | 2890542 | | bind/npm | 6,465,477 | 154.6676114250853 | ±0.79% | 3232739 | | dedupe/local | 2,016,470 | 495.91595590646887 | ±1.21% | 1008236 | | dedupe/npm | 1,790,586 | 558.476228165535 | ±1.22% | 895294 | Benchmarking 'arrays'. | Task Name | ops/sec | Average Time (ns) | Margin | Samples | | ------------- | --------- | ------------------ | ------ | ------- | | default/local | 4,663,846 | 214.4152840086707 | ±1.30% | 2331924 | | default/npm | 4,883,946 | 204.75246783717225 | ±1.32% | 2441974 | | bind/local | 3,607,460 | 277.20333455272043 | ±1.64% | 1803731 | | bind/npm | 4,454,320 | 224.50111620783923 | ±1.82% | 2227161 | | dedupe/local | 2,035,073 | 491.38275550207135 | ±0.70% | 1017537 | | dedupe/npm | 1,872,396 | 534.0747731186689 | ±0.69% | 936199 | Finished! ```
jonkoops commented 10 months ago

Benchmarks (compared to main)

This yields a nice little bump in performance, or otherwise remains flat. The following benchmarks were ran on my 2021 MacBook Pro 14-inch with an M1 Pro chip running macOS Sonoma (14.2.1):

Node.js (v21.6.0) ``` Benchmarking 'strings'. | Task Name | ops/sec | Average Time (ns) | Margin | Samples | | ------------- | ---------- | ------------------ | ------ | ------- | | default/local | 15,047,223 | 66.45744424462065 | ±0.38% | 7523612 | | default/main | 13,945,287 | 71.70881089010733 | ±0.61% | 6972644 | | bind/local | 12,214,449 | 81.87024925401062 | ±0.06% | 6107225 | | bind/main | 12,035,030 | 83.09077350183043 | ±0.54% | 6017516 | | dedupe/local | 4,783,401 | 209.05625995908 | ±1.05% | 2391701 | | dedupe/main | 4,729,184 | 211.45293629824653 | ±1.17% | 2364593 | Benchmarking 'object'. | Task Name | ops/sec | Average Time (ns) | Margin | Samples | | ------------- | ---------- | ------------------ | ------ | ------- | | default/local | 15,114,743 | 66.1605651857649 | ±0.66% | 7557373 | | default/main | 14,685,652 | 68.09366978133001 | ±0.45% | 7342827 | | bind/local | 12,373,718 | 80.81644760021568 | ±0.60% | 6186860 | | bind/main | 11,974,463 | 83.51104617267215 | ±0.92% | 5987232 | | dedupe/local | 9,282,668 | 107.72763806122742 | ±1.15% | 4641335 | | dedupe/main | 9,251,190 | 108.09419694201465 | ±1.32% | 4625596 | Benchmarking 'strings, object'. | Task Name | ops/sec | Average Time (ns) | Margin | Samples | | ------------- | ---------- | ------------------ | ------ | ------- | | default/local | 11,973,773 | 83.51586074730577 | ±0.48% | 5986887 | | default/main | 11,566,898 | 86.45360105117395 | ±0.75% | 5783450 | | bind/local | 10,103,948 | 98.97120927907973 | ±0.85% | 5051975 | | bind/main | 9,785,934 | 102.18747659924279 | ±1.09% | 4892968 | | dedupe/local | 4,743,135 | 210.8310071656631 | ±1.77% | 2371568 | | dedupe/main | 4,725,765 | 211.60594705772726 | ±1.84% | 2362883 | Benchmarking 'mix'. | Task Name | ops/sec | Average Time (ns) | Margin | Samples | | ------------- | --------- | ------------------ | ------ | ------- | | default/local | 6,689,803 | 149.481229121601 | ±1.73% | 3346487 | | default/main | 6,945,995 | 143.96783758643042 | ±0.48% | 3472998 | | bind/local | 5,802,990 | 172.3249303113438 | ±0.56% | 2901496 | | bind/main | 5,735,414 | 174.35531825465847 | ±0.68% | 2867708 | | dedupe/local | 2,149,263 | 465.2756748356308 | ±0.91% | 1074632 | | dedupe/main | 2,146,429 | 465.88999501424007 | ±0.96% | 1073215 | Benchmarking 'arrays'. | Task Name | ops/sec | Average Time (ns) | Margin | Samples | | ------------- | --------- | ------------------ | ------ | ------- | | default/local | 4,722,137 | 211.76851968384622 | ±0.89% | 2361069 | | default/main | 4,629,502 | 216.0059332482494 | ±0.98% | 2314752 | | bind/local | 3,858,260 | 259.1841518273684 | ±0.80% | 1929131 | | bind/main | 3,775,446 | 264.8693590799597 | ±0.86% | 1887724 | | dedupe/local | 2,156,157 | 463.78790793615264 | ±0.49% | 1078079 | | dedupe/main | 2,181,511 | 458.3978066592704 | ±0.55% | 1090756 | ```
Chrome (v120.0.6099.234) ``` Benchmarking 'strings'. | Task Name | ops/sec | Average Time (ns) | Margin | Samples | | ------------- | --------- | ------------------ | ------ | ------- | | default/local | 5,964,974 | 167.64532418749855 | ±2.79% | 2982487 | | default/main | 5,953,495 | 167.96853479532066 | ±2.78% | 2976748 | | bind/local | 5,747,027 | 174.00298032758442 | ±2.77% | 2873514 | | bind/main | 5,597,105 | 178.66375945596306 | ±2.77% | 2798553 | | dedupe/local | 3,401,317 | 294.0036774023779 | ±2.77% | 1700659 | | dedupe/main | 3,348,741 | 298.61960104228024 | ±2.78% | 1674371 | Benchmarking 'object'. | Task Name | ops/sec | Average Time (ns) | Margin | Samples | | ------------- | --------- | ------------------ | ------ | ------- | | default/local | 6,411,469 | 155.97047881618573 | ±2.77% | 3206376 | | default/main | 6,218,530 | 160.80970143364522 | ±2.77% | 3109887 | | bind/local | 6,173,953 | 161.97075652014624 | ±2.77% | 3086977 | | bind/main | 6,076,551 | 164.5670110298489 | ±2.79% | 3038276 | | dedupe/local | 5,253,729 | 190.3409577590494 | ±2.77% | 2626865 | | dedupe/main | 5,167,731 | 193.50848689738086 | ±2.77% | 2583866 | Benchmarking 'strings, object'. | Task Name | ops/sec | Average Time (ns) | Margin | Samples | | ------------- | --------- | ------------------ | ------ | ------- | | default/local | 5,778,725 | 173.04852315843544 | ±2.77% | 2889363 | | default/main | 5,777,019 | 173.09962576699212 | ±2.78% | 2888510 | | bind/local | 5,288,166 | 189.10146364234143 | ±2.78% | 2644612 | | bind/main | 5,129,733 | 194.9418819861671 | ±2.78% | 2564867 | | dedupe/local | 3,443,125 | 290.43380389114316 | ±2.79% | 1721907 | | dedupe/main | 3,431,338 | 291.43150572750335 | ±2.79% | 1715669 | Benchmarking 'mix'. | Task Name | ops/sec | Average Time (ns) | Margin | Samples | | ------------- | --------- | ------------------ | ------ | ------- | | default/local | 4,325,395 | 231.19270467399448 | ±2.86% | 2162698 | | default/main | 4,366,074 | 229.0386787206997 | ±2.77% | 2183474 | | bind/local | 4,008,818 | 249.45007257155387 | ±2.77% | 2004810 | | bind/main | 3,927,848 | 254.59230865702287 | ±2.77% | 1964317 | | dedupe/local | 2,027,646 | 493.1826205507442 | ±2.78% | 1014026 | | dedupe/main | 2,042,207 | 489.6661927386748 | ±2.77% | 1021308 | Benchmarking 'arrays'. | Task Name | ops/sec | Average Time (ns) | Margin | Samples | | ------------- | --------- | ------------------ | ------ | ------- | | default/local | 3,476,757 | 287.6242752769477 | ±2.77% | 1738379 | | default/main | 3,477,136 | 287.59296020856647 | ±2.77% | 1738916 | | bind/local | 3,101,401 | 322.434850550118 | ±2.77% | 1551011 | | bind/main | 3,065,055 | 326.2583130824787 | ±2.77% | 1532528 | | dedupe/local | 1,796,493 | 556.6397661720034 | ±2.79% | 898247 | | dedupe/main | 1,839,968 | 543.4877109251669 | ±2.78% | 920168 | ```
Firefox (v121.0.1) ``` Benchmarking 'strings'. | Task Name | ops/sec | Average Time (ns) | Margin | Samples | | ------------- | ---------- | ------------------ | ------ | ------- | | default/local | 11.465.108 | 87.22115831791554 | ±8.77% | 5732554 | | default/main | 11.136.846 | 89.79202908974408 | ±8.76% | 5568423 | | bind/local | 10.219.426 | 97.85285396655351 | ±8.76% | 5109713 | | bind/main | 11.094.782 | 90.13246046655087 | ±8.76% | 5547391 | | dedupe/local | 4.203.334 | 237.9063857404622 | ±8.76% | 2101667 | | dedupe/main | 4.185.526 | 238.91859708911136 | ±8.76% | 2092763 | Benchmarking 'object'. | Task Name | ops/sec | Average Time (ns) | Margin | Samples | | ------------- | ---------- | ------------------ | ------ | ------- | | default/local | 10.493.010 | 95.30153883394756 | ±8.76% | 5246505 | | default/main | 9.373.580 | 106.68282555864461 | ±8.76% | 4686790 | | bind/local | 10.022.232 | 99.77817316541865 | ±8.76% | 5011116 | | bind/main | 9.164.446 | 109.11734326330256 | ±8.76% | 4582223 | | dedupe/local | 7.183.410 | 139.2096511266933 | ±8.76% | 3591705 | | dedupe/main | 7.326.840 | 136.4844871731879 | ±8.76% | 3663420 | Benchmarking 'strings, object'. | Task Name | ops/sec | Average Time (ns) | Margin | Samples | | ------------- | --------- | ------------------ | ------ | ------- | | default/local | 9.259.502 | 107.99716874622416 | ±8.76% | 4629751 | | default/main | 8.863.456 | 112.82280862002361 | ±8.76% | 4431728 | | bind/local | 8.572.080 | 116.65780067381547 | ±8.76% | 4286040 | | bind/main | 8.501.824 | 117.62181856505146 | ±8.76% | 4250912 | | dedupe/local | 4.356.026 | 229.56704115172866 | ±8.76% | 2178013 | | dedupe/main | 4.436.480 | 225.40392383150606 | ±8.76% | 2218240 | Benchmarking 'mix'. | Task Name | ops/sec | Average Time (ns) | Margin | Samples | | ------------- | --------- | ------------------ | ------- | ------- | | default/local | 3.601.774 | 277.6409624812662 | ±8.76% | 1800887 | | default/main | 3.571.072 | 280.0279579913258 | ±8.76% | 1785536 | | bind/local | 3.489.062 | 286.60998285499085 | ±8.76% | 1744531 | | bind/main | 3.604.630 | 277.42098356835515 | ±8.76% | 1802315 | | dedupe/local | 1.672.024 | 598.0775395568484 | ±11.95% | 836012 | | dedupe/main | 2.052.874 | 487.12195682735523 | ±9.04% | 1026437 | Benchmarking 'arrays'. | Task Name | ops/sec | Average Time (ns) | Margin | Samples | | ------------- | --------- | ------------------ | ------ | ------- | | default/local | 2.679.476 | 373.2072987405 | ±8.76% | 1339738 | | default/main | 2.682.902 | 372.7307221806835 | ±8.76% | 1341451 | | bind/local | 2.479.526 | 403.30288934255987 | ±8.76% | 1239763 | | bind/main | 2.442.700 | 409.38305972898837 | ±8.76% | 1221350 | | dedupe/local | 1.916.104 | 521.8923398729924 | ±8.76% | 958052 | | dedupe/main | 1.812.134 | 551.835570658682 | ±8.76% | 906067 | ```
Safari (v17.2.1) ``` Benchmarking 'strings'. | Task Name | ops/sec | Average Time (ns) | Margin | Samples | | ------------- | --------- | ------------------ | ------ | ------- | | default/local | 5,235,506 | 191.00346970571914 | ±8.76% | 2622989 | | default/main | 5,104,025 | 195.92376684602667 | ±8.76% | 2552013 | | bind/local | 5,453,743 | 183.3602750697529 | ±8.76% | 2726872 | | bind/main | 5,430,235 | 184.15407484080146 | ±8.76% | 2720548 | | dedupe/local | 2,181,620 | 458.3748096966525 | ±8.75% | 1092992 | | dedupe/main | 2,185,949 | 457.46700519227045 | ±8.76% | 1092975 | Benchmarking 'object'. | Task Name | ops/sec | Average Time (ns) | Margin | Samples | | ------------- | --------- | ------------------ | ------ | ------- | | default/local | 5,336,655 | 187.3832602288938 | ±8.76% | 2668328 | | default/main | 4,910,015 | 203.66532410485314 | ±8.76% | 2455008 | | bind/local | 5,085,804 | 196.62572979150036 | ±8.76% | 2547988 | | bind/main | 5,102,367 | 195.98745993209607 | ±8.76% | 2556286 | | dedupe/local | 3,615,730 | 276.56928226128986 | ±8.76% | 1811481 | | dedupe/main | 3,712,715 | 269.3445984018247 | ±8.76% | 1856358 | Benchmarking 'strings, object'. | Task Name | ops/sec | Average Time (ns) | Margin | Samples | | ------------- | --------- | ------------------ | ------ | ------- | | default/local | 4,144,595 | 241.2780401274615 | ±8.76% | 2072298 | | default/main | 4,143,005 | 241.37063764813016 | ±8.76% | 2071503 | | bind/local | 4,301,051 | 232.5012578318218 | ±8.76% | 2150526 | | bind/main | 4,021,520 | 248.6621381370184 | ±8.76% | 2014782 | | dedupe/local | 2,135,269 | 468.3249670489714 | ±8.75% | 1069770 | | dedupe/main | 2,178,368 | 459.0592590416312 | ±8.76% | 1089184 | Benchmarking 'mix'. | Task Name | ops/sec | Average Time (ns) | Margin | Samples | | ------------- | --------- | ------------------ | ------ | ------- | | default/local | 2,866,562 | 348.84983998862106 | ±8.76% | 1436148 | | default/main | 2,777,155 | 360.0806408141651 | ±8.76% | 1391355 | | bind/local | 2,782,021 | 359.450793625687 | ±8.76% | 1393793 | | bind/main | 2,715,275 | 368.28676086277875 | ±8.76% | 1360353 | | dedupe/local | 1,317,070 | 759.2610871100245 | ±8.76% | 658535 | | dedupe/main | 1,334,322 | 749.4442870611442 | ±8.76% | 667161 | Benchmarking 'arrays'. | Task Name | ops/sec | Average Time (ns) | Margin | Samples | | ------------- | --------- | ------------------ | ------ | ------- | | default/local | 1,443,078 | 692.9632355285023 | ±8.76% | 721539 | | default/main | 1,411,093 | 708.6700106442441 | ±8.76% | 705547 | | bind/local | 1,450,289 | 689.5175441613753 | ±8.75% | 726595 | | bind/main | 1,387,037 | 720.9607811754571 | ±8.76% | 693519 | | dedupe/local | 963,279 | 1038.1203597987808 | ±8.75% | 482603 | | dedupe/main | 973,151 | 1027.5887014568293 | ±8.76% | 486576 | ```
jonkoops commented 10 months ago

Thanks for this @noscripter, it's a nice little cleanup.