Repository navigation
perf(cva): assemble one argument array per component call - #418
Conversation
Deploying with
|
| Status | Name | Latest Commit | Preview URL | Updated (UTC) |
|---|---|---|---|---|
| ✅ Deployment successful! View logs |
cva | 54ea630 | Commit Preview URL Branch Preview URL |
Sep 13 2026, 01:16 PM |
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 |
578,963 ops/s ±0.13% | 591,417 ops/s ±0.24% | -2.1% |
cva (runtime)component call with props |
97,796 ops/s ±0.24% | 96,039 ops/s ±0.27% | +1.8% |
cx (runtime)class join |
5,482,480 ops/s ±0.25% | 6,252,066 ops/s ±0.08% | 🔴 -12.3% |
cva (static)component definition |
15,266,174 ops/s ±1.10% | 13,806,549 ops/s ±0.37% | 🟢 +10.6% |
compose (static + runtime)define, join and call |
493,026 ops/s ±0.25% | 479,015 ops/s ±0.37% | +2.9% |
cva
| Task | This PR | 1.0.0-beta.9 (beta) |
Δ |
|---|---|---|---|
cva (runtime)component call with defaults |
1,747,583 ops/s ±0.10% | 563,334 ops/s ±0.14% | 🟢 +210.2% |
cva (runtime)component call with props |
342,079 ops/s ±0.25% | 96,943 ops/s ±0.26% | 🟢 +252.9% |
cx (runtime)class join |
4,725,641 ops/s ±0.11% | 4,486,827 ops/s ±0.32% | 🟢 +5.3% |
cva (static)component definition |
1,630,551 ops/s ±0.44% | 1,501,456 ops/s ±0.21% | 🟢 +8.6% |
composes property (static + runtime)define, join and call |
438,480 ops/s ±1.87% | 237,274 ops/s ±0.22% | 🟢 +84.8% |
| Call component (base only, className, x20) | 926,610 ops/s ±0.24% | 567,383 ops/s ±0.37% | 🟢 +63.3% |
| Call component (base only, x20) | 2,061,909 ops/s ±0.08% | 850,374 ops/s ±0.32% | 🟢 +142.5% |
| Call component (base only) | 15,287,339 ops/s ±0.07% | 9,954,765 ops/s ±1.30% | 🟢 +53.6% |
| Call component (compound variants, mostly matching) | 491,301 ops/s ±0.27% | 139,694 ops/s ±0.32% | 🟢 +251.7% |
| Call component (compound variants, none matching) | 690,641 ops/s ±0.16% | 164,936 ops/s ±0.33% | 🟢 +318.7% |
| Call component (explicit undefined props) | 1,552,511 ops/s ±0.24% | 541,123 ops/s ±0.32% | 🟢 +186.9% |
| Call component (hook) | 1,480,958 ops/s ±0.18% | 459,476 ops/s ±0.25% | 🟢 +222.3% |
| Call component (megamorphic props) | 1,441,817 ops/s ±0.10% | 428,487 ops/s ±0.32% | 🟢 +236.5% |
| Call component (props and className) | 1,708,524 ops/s ±0.28% | 503,463 ops/s ±0.32% | 🟢 +239.4% |
| Call component (variants without compounds) | 2,994,258 ops/s ±0.16% | 1,942,756 ops/s ±0.28% | 🟢 +54.1% |
| Call composed component (nested) | 1,034,309 ops/s ±0.27% | 290,110 ops/s ±0.34% | 🟢 +256.5% |
| Call composed component (prebuilt) | 1,328,210 ops/s ±0.26% | 375,278 ops/s ±0.27% | 🟢 +253.9% |
| Design system render pass | 65,344 ops/s ±1.83% | 17,475 ops/s ±0.28% | 🟢 +273.9% |
| Join class names (x20) | 337,356 ops/s ±0.25% | 363,493 ops/s ±0.31% | 🔴 -7.2% |
Commit f9cc3df · Node v24.20.0 · linux x64 · 2026-09-13T13:17:28.474Z
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.
|
@claude review |
|
Claude finished @joe-bell's task in 5m 39s —— View job Claude finished reviewing
SummaryI traced the refactor in No correctness bugs found. The behavior is equivalent to the pre-PR runtime for every realistic and most edge-case inputs I traced by hand:
Two minor, non-blocking notes:
Nothing in |
Replace the per-call allocation patterns in the cva runtime: the Object.entries/filter/fromEntries props copy, the two default/props spreads rebuilt per composed child, the compoundVariants reduce with a spread accumulator, the per-compound rest destructure, and the spread calls into the concatenator. Each call now builds one output array with push and hands it to the concatenator once via Reflect.apply, which keeps options as the receiver. The class-name output and the exact argument stream the concatenator receives are unchanged for typed input. Config properties are read live per call, so replacing base, variants or compoundVariants after definition is still observed; hooks are read per call; composed children are detached from the array before being called and each receives its own props object; for...in loops are guarded with hasOwnProperty so inherited keys and getters are never read; merges use spread and a computed-key path for an own __proto__ key so authored input can never reparent an object. Bundle sizes are unchanged (index.cjs 1347 B, config.cjs 878 B, utils.cjs 333 B). Tests are additive: they pin the argument stream, the receiver, live config reads, own-key semantics, prototype safety and composition isolation. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Follow-up from the code-quality and readability reviews: brace every multiline body, use line comments for internal explanations and keep JSDoc for exported declarations, rename the internal concatenator call to cxArray, narrow the helper parameter types, and shorten the merge loop comments. pushClassProps now returns the array so both component tails are a single return, and the merge loop reads components.length directly; together those save 4 B. The runtime-semantics tests move from index.test.ts, which is byte-identical to main again, into core.test.ts beside the source they cover. No behaviour change: the class-name output, the concatenator argument stream and the parity probes are unchanged. Bundle: index.cjs 1346 B, config.cjs 879 B, utils.cjs 333 B. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Add a short Enhancements entry summarising the runtime speed-up with the CI benchmark numbers and a link to the full table. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Reword the What's New entry to compare against class-variance-authority rather than earlier betas, credit the inspiring pull request, and drop the CI details. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
A component with no composition, no variants and no compound variants (checked live on every call) reads base, class and className once and, when base is defined and no class prop is given, hands the concatenator a one- or two-element array literal instead of running the general assembly. Every other plain shape is assembled in the same branch; the general path is unchanged. Base-only calls measure about 28% faster and now outrun the stable package; components with variants but no compound variants pay the extra guard, about 6%. index.cjs grows to 1410 B, so its size-limit cap moves to 1.45 kB. The deprecated compose merge tests its value with a truthiness check, which is equivalent and 13 B smaller. Tests pin the argument stream for every plain shape, the live transitions out of the fast path, hooks and the receiver. What's New states the speed-up against 0.7 in percentages. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Rename the two locals to classValue and classNameValue, and give the fast path's hook and receiver tests names that say which path they cover. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
020a5da to
54ea630
Compare
Description
Note
Inspired by @fveracoechea's earlier performance PR (#281), which first showed how much the per-call spreads and object copies cost and pushed this project to track benchmarks in CI. It didn't land as-is, but the direction here owes a lot to it. Thank you!
Removes the intermediate per-call allocations from the
cvaruntime inpackages/cva/src/core.ts. Each component call now assembles one output array withpushand hands it to the concatenator once viaReflect.apply(options.cx, options, out). Gone: theObject.entries→filter→Object.fromEntriesprops copy, the default/props spreads rebuilt per composed child, thecompoundVariantsreducewith a spread accumulator, the per-compound rest destructure, and every spread call intocx.The class-name output and the exact argument stream the concatenator receives are unchanged for typed input, and the design keeps today's runtime contract explicitly:
base,variantsandcompoundVariantsare read off the authored object on every call, so replacing or mutating them after definition is still observed. Composed children come from the authoredcomposesarray itself. No caches, no lazy build, no precomputed output.cx:donekeeps its nullish-precedence overonComplete.this) and each receives its own props object.for...inis guarded withhasOwnPropertybefore the property is read, so inherited keys and getters are never touched, matching theObject.keys/Object.entriessemantics it replaces.cva()take a computed-key path for an own__proto__key, so authored input can never reparent an object.Plain-component fast path. A component with no composition, no
variantsand nocompoundVariants(checked live on every call) readsbase,classandclassNameonce and, whenbaseis defined and noclassprop is given, hands the concatenator a 1- or 2-element array literal instead of running the general assembly. Measured: base-only calls +27% to +32% against the previous commit, and 1.2x the stable package where the beta was previously 0.36x. The price is one extra guard on every call: components with variants but no compound variants measure about 6% slower than the previous commit (still +95% againstbeta.9); other rows are within noise. Thecxwrapper was left unchanged after measuring that a scan-before-filter is slower on calls that contain anundefined.Bundle sizes:
index.cjs1410 B (was 1347 B onmain; its cap moves from 1.35 kB to 1.45 kB),config.cjs945 B (was 907 B, cap unchanged at 1.05 kB),utils.cjs333 B (unchanged). Types are untouched. Existing tests are untouched:index.test.tsis byte-identical tomain, and the new tests incore.test.tspin the argument stream, the receiver, live config reads, own-key semantics, prototype safety, composition isolation and the fast path.AGENTS.mdandREVIEW.mdare updated to describe the new runtime shape, and the beta docs' What's New page gains a short "Faster component calls" entry that links to this PR.Benchmarks
Local
pnpm bench:previewon this commit against1.0.0-beta.9(Node 24, linux x64). The CI sticky comment reproduces this on the runner.1.0.0-beta.9(beta)cva(runtime)component call with defaults
cva(runtime)component call with props
cx(runtime)class join
cva(static)component definition
composesproperty (static + runtime)define, join and call
Compared with
class-variance-authority@0.7.1Same scenarios, each package in its own authoring form, identical outputs, Node 24 (composition rows omitted; the stable package has no
composes).class-variance-authority@0.7.1cva(this PR)The
cxrow predates this PR (cva@1.0.0-beta.9sits at the same level) and comes from the beta'scxcontract:undefinedis filtered for narrow concatenators, hooks are honoured, andReflect.applykeeps the receiver, where the stable package'scxisclsxitself. A scan-before-filter was measured slower on the benchmarked calls, so it stays. The base-only row was 0.36x before the fast path. A CI preview showedJoin class names (x20)at -8.6% on one run although that code is unchanged; five interleaved re-runs against the previous build range 0.95x to 1.05x, so treat it as noise.Additional context
Three edge behaviours differ from the previous runtime, deliberately, and none is reachable through documented, typed usage:
compose()no longer forwards an own key whose value is explicitlyundefinedto its children. Output is identical for anycvacomponent, since an explicitundefinedalready falls back to the default.__proto__variant key (computed key orJSON.parse) on a composed config is now kept as an own data property incomponent.config.variants. The previous runtime silently dropped it as a side effect of resetting a reparented accumulator. Deprecatedcompose()'s config merge is unchanged and still assigns per key.composes/compoundVariantsarrays now throw rather than being skipped: a hole incomposesthrows whencva()runs, a hole incompoundVariantsthrows on the first component call that reaches it. TypeScript rejects literal holes; onlyany-typed values such asnew Array(n)get through, and no documented usage does.Getter invocation counts and ordering on props or config objects are not part of the contract and may differ.
Reviewers may want to focus on
definedProps, the compound-variant loop and the definition-time merge loop incore.ts, and oncore.test.ts.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
Open workspace in Conductor