Repository navigation
perf(cva): cut the type instantiations a component costs - #420
Merged
Merged
Conversation
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>
Deploying with
|
| 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>
Contributor
BenchmarksComparing this PR's local benchmark run against the latest published npm versions. Aim for higher ops/s. Treat deltas within ±5% as noise.
|
| 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.
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
marked this pull request as ready for review
September 13, 2026 19:54
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>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes JB-386
Description
The authoring types in
packages/cva/src/core.tsdo less work per component. Same API, same accept/reject behaviour (with one intentional exception below), same inferred prop andgetSchematypes, and a consumer's own emitted declarations are byte-identical to amainbuild. The runtime region of the file, the bundle (2117 / 1652 / 333 B) and the size caps are untouched.Five changes, from a measured prototype:
compoundVariantsentries 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.CVACompoundVariants<V, T>) so TypeScript caches one instantiation per(V, T)instead of rebuilding the element type at every use.AllVariantsanswers the no-composescase withVariantsverbatim, skippingComposedTuple,MergedVariantsand itsUnionToIntersection.VariantPropsis a singleOmitinstead of two nested ones.GetSchemabuilds 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:maintwMergeVariantPropsdirectlygetSchemacall eachCheck 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
getSchemacall sites there). On thegetSchemafixture 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) andVariantProps' body and the use sites change; the export list is identical. The consumer fixtures gained a props interface extendingVariantPropsand a composed component withcompoundVariants; their packed declarations are byte-identical to amainbuild, so nothing new is exported.Tests
Four tests appended to
core.test.tsunder "authoring types" (direct interface extension,VariantPropsomissions,__proto__rejection in literal and computed form, broad record accepted).index.test.ts,config.test.tsandutils.test.tsare byte-identical tomain. 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.mdandREVIEW.mdrecord the type-performance constraints. Removing the deprecated APIs and the bundle-size pass follow separately.What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123).🤖 Generated with Claude Code