Skip to content

perf(cva): cut the type instantiations a component costs - #420

Merged
joe-bell merged 5 commits into
mainfrom
jb-386-typescript-perf
Sep 13, 2026
Merged

joe-bell merged 5 commits into
mainfrom
jb-386-typescript-perf

Conversation

@joe-bell

@joe-bell joe-bell commented Sep 13, 2026 •

Copy link
Copy Markdown
Owner

Closes JB-386

Description

The authoring types in packages/cva/src/core.ts do less work per component. Same API, same accept/reject behaviour (with one intentional exception below), same inferred prop and getSchema types, and a consumer's own emitted declarations are byte-identical to a main build. The runtime region of the file, the bundle (2117 / 1652 / 333 B) and the size caps are untouched.

Five changes, from a measured prototype:

  1. compoundVariants entries are one mapped type (CVACompoundVariantSchema, a single value or a list per variant) instead of a two-arm union whose second arm already subsumed the first.
  2. The compound-variants array type is a named alias (CVACompoundVariants<V, T>) so TypeScript caches one instantiation per (V, T) instead of rebuilding the element type at every use.
  3. AllVariants answers the no-composes case with Variants verbatim, skipping ComposedTuple, MergedVariants and its UnionToIntersection.
  4. VariantProps is a single Omit instead of two nested ones.
  5. GetSchema builds each entry once (SchemaEntry) and drops empty entries while mapping the keys, instead of building the schema and re-mapping it.

Two guarantees pinned by tests: interface ButtonProps extends VariantProps<typeof button> {} keeps compiling, and a statically visible __proto__ variant name is a compile error (variants?: Variants & { __proto__?: never }). A variants value widened to a broad record still compiles, and a test pins that boundary. Both live in code comments and tests, not the docs.

Measurements

tsc --extendedDiagnostics, TypeScript 6, Node 24, median of three, instantiations, main → this PR:

Fixture main This PR Δ
200 components 516,334 380,052 −26.4%
200 components under twMerge 515,569 379,514 −26.4%
50 components, props interface extending VariantProps directly 132,484 98,602 −25.6%
50 components with a getSchema call each 153,772 116,661 −24.1%
50 composed components 184,295 155,135 −15.8%
One component 7,093 6,330 −10.8%

Check time on the 200-component fixture, median of seven: 1.80 s → 1.61 s. Per change in isolation on that fixture: 1 −19.1%, 2 −3.0%, 3 −8.2%, 4 −1.6%, 5 −0.002% (no getSchema call sites there). On the getSchema fixture the 50 call sites cost 21,288 → 18,309 instantiations across the whole PR (−14%), of which change 5 alone accounts for −13%. The __proto__ guard costs +404 instantiations (+0.1%).

Declarations

Four private aliases appear in the shared core-*.d.mts (CVACompoundVariantSchema, CVACompoundVariants, AllVariants, SchemaEntry) and VariantProps' body and the use sites change; the export list is identical. The consumer fixtures gained a props interface extending VariantProps and a composed component with compoundVariants; their packed declarations are byte-identical to a main build, so nothing new is exported.

Tests

Four tests appended to core.test.ts under "authoring types" (direct interface extension, VariantProps omissions, __proto__ rejection in literal and computed form, broad record accepted). index.test.ts, config.test.ts and utils.test.ts are byte-identical to main. Coverage stays at 100%.

Additional context

Last PR of the performance stream after #418 and #419. What's New gains a short entry 7; AGENTS.md and REVIEW.md record the type-performance constraints. Removing the deprecated APIs and the bundle-size pass follow separately.


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Follow the Style Guide.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).

🤖 Generated with Claude Code

Reshape the authoring types so TypeScript does less work per component
with the same accept/reject behaviour, inferred types and consumer
declarations: one mapped `compoundVariants` schema behind a named array
alias, an `AllVariants` shortcut when nothing is composed, a single
`Omit` in `VariantProps`, and a single-pass `GetSchema` via
`SchemaEntry`. A statically visible `__proto__` variant name is now a
compile error, and a test pins that `interface X extends
VariantProps<typeof c>` keeps compiling.

Measured with `tsc --extendedDiagnostics` (median of three): 200
components 516,334 -> 380,052 instantiations, check 1.80 -> 1.61 s;
50 components with a `getSchema` call 153,772 -> 116,661; 50 composed
184,295 -> 155,135; 50 shadcn-style interfaces 132,484 -> 98,602.
Bundle bytes unchanged.

Closes JB-386

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Sep 13, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
cva 06e4be1 Commit Preview URL

Branch Preview URL
Sep 13 2026, 07:54 PM

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Benchmarks

Comparing this PR's local benchmark run against the latest published npm versions.

Aim for higher ops/s. Treat deltas within ±5% as noise.

class-variance-authority

Note

class-variance-authority is in maintenance mode, and its latest npm baseline predates the tsdown build migration. Treat the deltas here as indicative.

Task This PR 0.7.1 Δ
cva (runtime)
component call with defaults
605,714 ops/s ±0.14% 600,153 ops/s ±0.24% +0.9%
cva (runtime)
component call with props
95,360 ops/s ±0.25% 88,919 ops/s ±0.25% 🟢 +7.2%
cx (runtime)
class join
5,946,430 ops/s ±0.29% 5,816,475 ops/s ±0.08% +2.2%
cva (static)
component definition
15,435,340 ops/s ±0.09% 13,188,721 ops/s ±0.45% 🟢 +17.0%
compose (static + runtime)
define, join and call
498,837 ops/s ±0.26% 497,640 ops/s ±0.29% +0.2%

cva

Task This PR 1.0.0-beta.9 (beta) Δ
cva (runtime)
component call with defaults
3,330,076 ops/s ±0.13% 553,260 ops/s ±0.15% 🟢 +501.9%
cva (runtime)
component call with props
404,859 ops/s ±0.14% 87,258 ops/s ±0.30% 🟢 +364.0%
cx (runtime)
class join
4,688,988 ops/s ±0.12% 4,327,692 ops/s ±0.42% 🟢 +8.3%
cva (static)
component definition
663,393 ops/s ±0.60% 1,471,374 ops/s ±0.24% 🔴 -54.9%
composes property (static + runtime)
define, join and call
304,345 ops/s ±0.30% 235,172 ops/s ±0.31% 🟢 +29.4%
Call component (base only, className, x20) 1,056,609 ops/s ±6.53% 563,640 ops/s ±0.48% 🟢 +87.5%
Call component (base only, x20) 5,666,001 ops/s ±0.15% 854,986 ops/s ±0.47% 🟢 +562.7%
Call component (base only) 18,548,221 ops/s ±0.08% 9,692,410 ops/s ±0.79% 🟢 +91.4%
Call component (compound variants, mostly matching) 834,675 ops/s ±0.26% 136,288 ops/s ±0.38% 🟢 +512.4%
Call component (compound variants, none matching) 1,265,302 ops/s ±0.25% 161,631 ops/s ±0.34% 🟢 +682.8%
Call component (explicit undefined props) 2,802,150 ops/s ±0.29% 517,645 ops/s ±0.32% 🟢 +441.3%
Call component (hook) 1,627,554 ops/s ±0.26% 421,477 ops/s ±0.35% 🟢 +286.2%
Call component (megamorphic props) 1,587,761 ops/s ±0.34% 376,544 ops/s ±0.30% 🟢 +321.7%
Call component (props and className) 1,723,159 ops/s ±0.29% 484,106 ops/s ±0.36% 🟢 +255.9%
Call component (variants without compounds) 3,009,203 ops/s ±0.34% 1,903,491 ops/s ±0.38% 🟢 +58.1%
Call composed component (nested) 960,465 ops/s ±0.32% 295,904 ops/s ±0.40% 🟢 +224.6%
Call composed component (prebuilt) 1,293,178 ops/s ±0.24% 372,700 ops/s ±0.37% 🟢 +247.0%
Design system render pass 88,407 ops/s ±0.30% 17,292 ops/s ±0.33% 🟢 +411.2%
Join class names (x20) 326,205 ops/s ±1.96% 343,993 ops/s ±0.45% 🔴 -5.2%

Commit 47868ed · Node v24.20.0 · linux x64 · 2026-09-13T19:55:17.053Z

These benchmarks ran in CI on this PR's code. The comment checks that the report looks valid before posting it, but it cannot guarantee the numbers are correct. If you see a large change, re-run the benchmarks locally before treating it as a real improvement or regression.

joe-bell and others added 3 commits September 13, 2026 18:13
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Keep the What's New entry to the speed-up in plain terms, drop the
second credit line, and move the `__proto__` and interface notes into
code comments. Shorten the new type comments and guidance bullets.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@joe-bell
joe-bell marked this pull request as ready for review September 13, 2026 19:54
@joe-bell
joe-bell merged commit 67af426 into main Sep 13, 2026
11 checks passed
joe-bell added a commit that referenced this pull request Sep 20, 2026
`REVIEW.md` and `AGENTS.md` both require an instantiation measurement before
reshaping `packages/cva`'s authoring types, but nothing enforced it and past
numbers survive only as PR prose, so every author reconstructed the method.

Add `pnpm check:type-performance`, which compiles five fixtures (plain,
`twMerge`, direct-interface, `getSchema`, composed) against the built
`packages/cva/dist/*.d.mts` declarations and fails on any difference from the
committed baseline in either direction, on a tsc diagnostic in a fixture, on a
fixture/baseline mismatch, or on a TypeScript or `tailwind-merge` version that
no longer matches the baseline. Measuring built declarations rather than `src`
keeps the count on consumer cost: a `src` compile bundles in the runtime
program's own ~62k instantiations, which move on every unrelated refactor.

It joins `pnpm check`, so it runs in CI's already-required `check` context with
no new required check. `lint-staged` skips root scripts, so `CONTRIBUTING.md`
tells authors to run it by hand after an authoring-type change.

Replace the private `Uninferred<T>` helper with TypeScript's built-in
`NoInfer<T>`. The `AGENTS.md` entry claiming native `NoInfer` regresses literal
defaults and `getSchema` assertions predates the `CVAComponentConfig` rewrites
in #420 and #423; on the current shape it compiles clean against every
assertion in the suite (182,165 -> 181,465 instantiations). `NoInfer` needs
TypeScript >= 5.4 in a consumer, and the package already peers `>= 6.0.0`.

Add an `--exactOptionalPropertyTypes` compile to `test:consumer`, pinning that
`button({ class: "x", className: undefined })` is accepted under plain
`--strict` and rejected once that flag is set, while every other
optional-property case stays accepted in both modes.

Also correct `REVIEW.md`'s stale `cva/tools` size cap: it said `0.35KB`, the
budget is `0.4KB`.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
joe-bell added a commit that referenced this pull request Sep 20, 2026
* feat(cva): gate authoring-type instantiation counts

`REVIEW.md` and `AGENTS.md` both require an instantiation measurement before
reshaping `packages/cva`'s authoring types, but nothing enforced it and past
numbers survive only as PR prose, so every author reconstructed the method.

Add `pnpm check:type-performance`, which compiles five fixtures (plain,
`twMerge`, direct-interface, `getSchema`, composed) against the built
`packages/cva/dist/*.d.mts` declarations and fails on any difference from the
committed baseline in either direction, on a tsc diagnostic in a fixture, on a
fixture/baseline mismatch, or on a TypeScript or `tailwind-merge` version that
no longer matches the baseline. Measuring built declarations rather than `src`
keeps the count on consumer cost: a `src` compile bundles in the runtime
program's own ~62k instantiations, which move on every unrelated refactor.

It joins `pnpm check`, so it runs in CI's already-required `check` context with
no new required check. `lint-staged` skips root scripts, so `CONTRIBUTING.md`
tells authors to run it by hand after an authoring-type change.

Replace the private `Uninferred<T>` helper with TypeScript's built-in
`NoInfer<T>`. The `AGENTS.md` entry claiming native `NoInfer` regresses literal
defaults and `getSchema` assertions predates the `CVAComponentConfig` rewrites
in #420 and #423; on the current shape it compiles clean against every
assertion in the suite (182,165 -> 181,465 instantiations). `NoInfer` needs
TypeScript >= 5.4 in a consumer, and the package already peers `>= 6.0.0`.

Add an `--exactOptionalPropertyTypes` compile to `test:consumer`, pinning that
`button({ class: "x", className: undefined })` is accepted under plain
`--strict` and rejected once that flag is set, while every other
optional-property case stays accepted in both modes.

Also correct `REVIEW.md`'s stale `cva/tools` size cap: it said `0.35KB`, the
budget is `0.4KB`.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(cva): close two type-performance gate holes found in review

No fixture authored `compoundVariants`, so the gate did not protect the
surface this branch changes (`CVACompoundVariants<NoInfer<Merged>, T>`), which
AGENTS.md names as performance-sensitive. A two-arm-union regression in
`CVACompoundVariantSchema` moved a consumer from 2102 to 2326 instantiations
while every committed count stayed identical. Add scalar and array-valued
compound entries to the plain and composed fixtures, the latter selecting on
composed keys, and re-baseline: plain 635 -> 2202, composed 3945 -> 5522.

`compileFixture` discarded the compiler's exit status, so a process that
printed a well-formed `Instantiations:` line and then failed was
indistinguishable from success — an exact baseline match with zero `error TS`
lines and a non-zero exit passed the gate. Track process failure independently
of parsed output, reject it before the count is compared, and keep stderr in
the message.

Also check `schemaVersion`, reject an empty fixture set, and assert the exact
compiler argv so dropping `--noEmit` cannot silently start emitting files
beside the fixtures.

Correct the documentation the same review found overstated. The `AGENTS.md`
entry no longer invents a history for dropping `Uninferred`: the claimed
regression does not reproduce under `tsc` at any commit that carried the
entry, including the one that introduced it, and no cause is asserted because
none is evidenced. `REVIEW.md` narrows its mechanised claim to
optional-property acceptance and restores "same consumer declarations" as a
manual criterion, because the new lane is `--noEmit` and cannot pin
declaration portability. `CONTRIBUTING.md` no longer contradicts itself about
`tailwind-merge`, which is the consumer's own root dependency. The
`composed.mts` and `exact-optional.*` comments now describe what those
fixtures actually check.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* refactor: simplify type performance measurements

* perf(cva): use native NoInfer without benchmark tooling

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
@joe-bell
joe-bell deleted the jb-386-typescript-perf branch November 20, 2026 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant