-
-
Notifications
You must be signed in to change notification settings - Fork 135
Feat: Performance enhancements #281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fveracoechea
wants to merge
13
commits into
joe-bell:main
Choose a base branch
from
fveracoechea:feat/performance-enhancements
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+555
−72
Open
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ac954b6
chore: backup original implementation
fveracoechea a80aa7c
feat: add compound variants type
fveracoechea ffb8b11
feat: implement performance enhancements
fveracoechea c920bbf
cleanup
fveracoechea 7a3d8a4
feat: update benchmarks
fveracoechea 749ba1e
Merge branch 'main' into feat/performance-enhancements
fveracoechea ad7e196
Add tinybench dev dependency
fveracoechea 2a7e708
Add early return when no variants are provided
fveracoechea dd9e8b1
Merge branch 'main' into feat/performance-enhancements
fveracoechea 5050a21
Merge branch 'main' into feat/performance-enhancements
joe-bell 17ef0b7
Merge branch 'main' into feat/performance-enhancements
fveracoechea bb3b998
Merge branch 'main' into feat/performance-enhancements
fveracoechea fea57ba
Merge branch 'main' into feat/performance-enhancements
fveracoechea File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| import { Bench } from "tinybench"; | ||
|
|
||
| import { defineConfig as originalDefineConfig } from "./old-index"; | ||
| import { defineConfig as enhancedDefineConfig } from "./index"; | ||
|
|
||
| function logResultsTable(bench: Bench) { | ||
| const table: any[] = []; | ||
|
|
||
| for (const task of bench.tasks) { | ||
| if (!task.result) continue; | ||
| table.push({ | ||
| name: task.name, | ||
| "ops/sec": task.result.error ? 0 : task.result.hz, | ||
| "average (ms)": task.result.error ? "NaN" : task.result.mean.toFixed(3), | ||
| samples: task.result.error ? "NaN" : task.result.samples.length, | ||
| }); | ||
| } | ||
|
|
||
| const results = table | ||
| .map((x) => ({ | ||
| ...x, | ||
| "ops/sec": parseFloat(parseInt(x["ops/sec"].toString(), 10).toString()), | ||
| })) | ||
| .sort( | ||
| (a: Record<string, number>, b: Record<string, number>) => | ||
| b["ops/sec"] - a["ops/sec"], | ||
| ); | ||
|
|
||
| const maxOps = Math.max(...results.map((x) => x["ops/sec"])); | ||
|
|
||
| console.table( | ||
| results.map((x, i) => ({ | ||
| ...x, | ||
| [`relative to ${results[0]["name"]}`]: | ||
| i === 0 | ||
| ? "" | ||
| : `${(maxOps / parseInt(x["ops/sec"])).toFixed(2)} x slower`, | ||
| })), | ||
| ); | ||
| } | ||
|
|
||
| const originalCVA = originalDefineConfig().cva; | ||
| const enhancedCVA = enhancedDefineConfig().cva; | ||
|
|
||
| const buttonWithoutBaseWithDefaultsWithClassNameString = { | ||
| base: "button font-semibold border rounded", | ||
| variants: { | ||
| intent: { | ||
| unset: null, | ||
| primary: | ||
| "button--primary bg-blue-500 text-white border-transparent hover:bg-blue-600", | ||
| secondary: | ||
| "button--secondary bg-white text-gray-800 border-gray-400 hover:bg-gray-100", | ||
| warning: | ||
| "button--warning bg-yellow-500 border-transparent hover:bg-yellow-600", | ||
| danger: [ | ||
| "button--danger", | ||
| [ | ||
| 1 && "bg-red-500", | ||
| { baz: false, bat: null }, | ||
| ["text-white", ["border-transparent"]], | ||
| ], | ||
| "hover:bg-red-600", | ||
| ], | ||
| }, | ||
| disabled: { | ||
| unset: null, | ||
| true: "button--disabled opacity-050 cursor-not-allowed", | ||
| false: "button--enabled cursor-pointer", | ||
| }, | ||
| size: { | ||
| unset: null, | ||
| small: "button--small text-sm py-1 px-2", | ||
| medium: "button--medium text-base py-2 px-4", | ||
| large: "button--large text-lg py-2.5 px-4", | ||
| }, | ||
| m: { | ||
| unset: null, | ||
| 0: "m-0", | ||
| 1: "m-1", | ||
| }, | ||
| }, | ||
| compoundVariants: [ | ||
| { | ||
| intent: "primary", | ||
| size: "medium", | ||
| className: "button--primary-medium uppercase", | ||
| }, | ||
| { | ||
| intent: "warning", | ||
| disabled: false, | ||
| className: "button--warning-enabled text-gray-800", | ||
| }, | ||
| { | ||
| intent: "warning", | ||
| disabled: true, | ||
| className: [ | ||
| "button--warning-disabled", | ||
| [1 && "text-black", { baz: false, bat: null }], | ||
| ], | ||
| }, | ||
| { | ||
| intent: ["warning", "danger"], | ||
| className: "button--warning-danger !border-red-500", | ||
| }, | ||
| { | ||
| intent: ["warning", "danger"], | ||
| size: "medium", | ||
| className: "button--warning-danger-medium", | ||
| }, | ||
| ], | ||
| defaultVariants: { | ||
| m: 0, | ||
| disabled: false, | ||
| intent: "primary", | ||
| size: "medium", | ||
| }, | ||
| } as any; | ||
|
|
||
| async function run() { | ||
| const benchmark = new Bench({ time: 5000 }); | ||
|
|
||
| benchmark.add("cva/main", () => { | ||
| const buttonVariants = originalCVA( | ||
| buttonWithoutBaseWithDefaultsWithClassNameString, | ||
| ); | ||
| buttonVariants({}); | ||
| buttonVariants({ intent: "primary", disabled: true } as any); | ||
| buttonVariants({ intent: "primary", size: "medium" } as any); | ||
| buttonVariants({ | ||
| intent: "warning", | ||
| size: "medium", | ||
| disabled: true, | ||
| } as any); | ||
| buttonVariants({ size: "small" } as any); | ||
| buttonVariants({ size: "large", intent: "unset" } as any); | ||
| }); | ||
|
|
||
| benchmark.add("feat/performance-enhancement", () => { | ||
| const buttonVariants = enhancedCVA( | ||
| buttonWithoutBaseWithDefaultsWithClassNameString, | ||
| ); | ||
| buttonVariants({}); | ||
| buttonVariants({ intent: "primary", disabled: true } as any); | ||
| buttonVariants({ intent: "primary", size: "medium" } as any); | ||
| buttonVariants({ | ||
| intent: "warning", | ||
| size: "medium", | ||
| disabled: true, | ||
| } as any); | ||
| buttonVariants({ size: "small" } as any); | ||
| buttonVariants({ size: "large", intent: "unset" } as any); | ||
| }); | ||
|
|
||
| await benchmark.warmup(); | ||
| await benchmark.run(); | ||
| logResultsTable(benchmark); | ||
|
|
||
| process.exit(); | ||
| } | ||
|
|
||
| run(); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to mention, this file is just for benchmarking purposes, basically making sure these changes are actually faster, could be removed before merging.