Skip to content

perf(cva): assemble one argument array per component call - #418

Merged
joe-bell merged 7 commits into
mainfrom
perf-cva-runtime
Sep 13, 2026
Merged

joe-bell merged 7 commits into
mainfrom
perf-cva-runtime

Conversation

@joe-bell

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

Copy link
Copy Markdown
Owner

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 cva runtime in packages/cva/src/core.ts. Each component call now assembles one output array with push and hands it to the concatenator once via Reflect.apply(options.cx, options, out). Gone: the Object.entries → filter → Object.fromEntries props copy, the default/props spreads rebuilt per composed child, the compoundVariants reduce with a spread accumulator, the per-compound rest destructure, and every spread call into cx.

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:

  • Config is read live per call. base, variants and compoundVariants are read off the authored object on every call, so replacing or mutating them after definition is still observed. Composed children come from the authored composes array itself. No caches, no lazy build, no precomputed output.
  • Hooks are read per call, and cx:done keeps its nullish-precedence over onComplete.
  • Composed children are detached from the array before being called (so the array never arrives as this) and each receives its own props object.
  • Own-key semantics. Every for...in is guarded with hasOwnProperty before the property is read, so inherited keys and getters are never touched, matching the Object.keys/Object.entries semantics it replaces.
  • Prototype safety. Merges use spread, and the two per-key merges in 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 variants and no compoundVariants (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 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% against beta.9); other rows are within noise. The cx wrapper was left unchanged after measuring that a scan-before-filter is slower on calls that contain an undefined.

Bundle sizes: index.cjs 1410 B (was 1347 B on main; its cap moves from 1.35 kB to 1.45 kB), config.cjs 945 B (was 907 B, cap unchanged at 1.05 kB), utils.cjs 333 B (unchanged). Types are untouched. Existing tests are untouched: index.test.ts is byte-identical to main, and the new tests in core.test.ts pin the argument stream, the receiver, live config reads, own-key semantics, prototype safety, composition isolation and the fast path. AGENTS.md and REVIEW.md are 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:preview on this commit against 1.0.0-beta.9 (Node 24, linux x64). The CI sticky comment reproduces this on the runner.

Task This PR 1.0.0-beta.9 (beta) Δ
cva (runtime)
component call with defaults
1,773,207 ops/s ±4.98% 467,766 ops/s ±1.30% 🟢 +279.1%
cva (runtime)
component call with props
347,104 ops/s ±1.34% 84,511 ops/s ±1.68% 🟢 +310.7%
cx (runtime)
class join
4,271,763 ops/s ±2.52% 4,098,235 ops/s ±1.19% +4.2%
cva (static)
component definition
1,697,465 ops/s ±0.42% 1,207,935 ops/s ±0.91% 🟢 +40.5%
composes property (static + runtime)
define, join and call
412,004 ops/s ±0.89% 218,677 ops/s ±0.55% 🟢 +88.4%
Call component (base only, className, x20) 591,885 ops/s ±0.92% 426,977 ops/s ±0.97% 🟢 +38.6%
Call component (base only, x20) 665,800 ops/s ±1.09% 545,725 ops/s ±0.81% 🟢 +22.0%
Call component (base only) 9,606,471 ops/s ±2.96% 8,907,643 ops/s ±1.83% 🟢 +7.8%
Call component (compound variants, mostly matching) 468,241 ops/s ±1.23% 113,060 ops/s ±0.78% 🟢 +314.2%
Call component (compound variants, none matching) 729,982 ops/s ±0.23% 147,781 ops/s ±0.68% 🟢 +394.0%
Call component (explicit undefined props) 1,612,361 ops/s ±3.45% 452,794 ops/s ±0.93% 🟢 +256.1%
Call component (hook) 1,597,017 ops/s ±0.27% 390,067 ops/s ±0.66% 🟢 +309.4%
Call component (megamorphic props) 1,506,884 ops/s ±0.45% 369,969 ops/s ±0.64% 🟢 +307.3%
Call component (props and className) 1,838,253 ops/s ±0.36% 429,975 ops/s ±0.65% 🟢 +327.5%
Call component (variants without compounds) 3,007,293 ops/s ±1.71% 1,649,586 ops/s ±1.60% 🟢 +82.3%
Call composed component (nested) 821,428 ops/s ±1.19% 263,137 ops/s ±1.09% 🟢 +212.2%
Call composed component (prebuilt) 1,168,937 ops/s ±0.39% 315,353 ops/s ±0.68% 🟢 +270.7%
Design system render pass 69,213 ops/s ±0.89% 16,277 ops/s ±1.25% 🟢 +325.2%
Join class names (x20) 323,626 ops/s ±0.52% 297,454 ops/s ±0.89% 🟢 +8.8%

Compared with class-variance-authority@0.7.1

Same scenarios, each package in its own authoring form, identical outputs, Node 24 (composition rows omitted; the stable package has no composes).

Scenario class-variance-authority@0.7.1 cva (this PR) Δ
Call component (default variants) 474,411 ops/s 1,741,584 ops/s 3.7x
Call component (with variants) 86,783 ops/s 348,332 ops/s 4.0x
Call component (variants without compounds) 2,135,344 ops/s 2,962,949 ops/s 1.4x
Call component (explicit undefined props) 488,321 ops/s 1,676,843 ops/s 3.4x
Call component (props and className) 449,928 ops/s 1,536,172 ops/s 3.4x
Call component (compound variants, mostly matching) 96,045 ops/s 493,992 ops/s 5.1x
Call component (compound variants, none matching) 105,898 ops/s 729,486 ops/s 6.9x
Call component (megamorphic props) 328,880 ops/s 1,480,984 ops/s 4.5x
Call component (base only, x20) 1,800,872 ops/s 2,159,532 ops/s 1.20x
Join class names (x20) 494,157 ops/s 347,629 ops/s 0.70x

The cx row predates this PR (cva@1.0.0-beta.9 sits at the same level) and comes from the beta's cx contract: undefined is filtered for narrow concatenators, hooks are honoured, and Reflect.apply keeps the receiver, where the stable package's cx is clsx itself. 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 showed Join 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:

  • Deprecated compose() no longer forwards an own key whose value is explicitly undefined to its children. Output is identical for any cva component, since an explicit undefined already falls back to the default.
  • A __proto__ variant key (computed key or JSON.parse) on a composed config is now kept as an own data property in component.config.variants. The previous runtime silently dropped it as a side effect of resetting a reparented accumulator. Deprecated compose()'s config merge is unchanged and still assigns per key.
  • Sparse composes / compoundVariants arrays now throw rather than being skipped: a hole in composes throws when cva() runs, a hole in compoundVariants throws on the first component call that reaches it. TypeScript rejects literal holes; only any-typed values such as new 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 in core.ts, and on core.test.ts.


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


Open workspace in Conductor

@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 54ea630 Commit Preview URL

Branch Preview URL
Sep 13 2026, 01:16 PM

@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
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.

Copy link
Copy Markdown
Owner Author

@claude review

@claude

claude Bot commented Sep 13, 2026 •

Copy link
Copy Markdown

Claude finished @joe-bell's task in 5m 39s —— View job


Claude finished reviewing

  • Read trigger comment and gather PR context
  • Review packages/cva/src/core.ts runtime changes
  • Review packages/cva/src/core.test.ts coverage
  • Review docs/AGENTS.md/REVIEW.md updates
  • Post final review feedback

Summary

I traced the refactor in packages/cva/src/core.ts (diffed against the branch's actual fork point, ba2e72d, since origin/main in this sandbox is a shallow ref with no common ancestor — the file set matches the PR stats exactly) against the previous implementation, focusing on the areas the PR description calls out: definedProps, the compound-variant loop, and the definition-time merge loop.

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:

  • definedProps's own-key + __proto__-safe merge produces the same overlay as the old Object.entries/Object.fromEntries filter.
  • The definition-time merge loop (children then authored, in order) preserves "local defaultVariants/variants win" and one-level-deep variant merging, matching the type-level contract in CVA.
  • The compound-variant loop's own-key for...in + hasOwn guard reproduces the old Object.entries(...).every(...) semantics, including a compound with no non-class selectors trivially matching.
  • The fast path's guard (!components.length && !compounds && !authored.variants, all checked live) only short-circuits when the general path would produce an identical argument stream; I couldn't find an input where it diverges.
  • __proto__ handling: the two new per-key merges (definedProps, the variant accumulator) correctly use a computed-key object literal ({ ...merged, [key]: value }) rather than merged[key] = value when key === "__proto__", which is what avoids invoking Object.prototype's __proto__ setter. I confirmed this is the right fix — a bracket assignment (obj[key] = value) does invoke that setter and reparent a plain object, unlike a computed-key literal or spread.
  • The new AGENTS.md line noting that deprecated compose()'s per-key config merge (config[key] = ...) is not proto-guarded and can reparent its own fresh config object is accurate — but it's genuinely pre-existing (the old reduce-based merge had the identical acc[key] = ... pattern), so this PR isn't introducing it, only documenting it. No action needed.

Two minor, non-blocking notes:

  1. The PR body states "Sparse composes / compoundVariants arrays now throw at definition rather than being skipped." For composes, that's right — a hole is read as components[i].config inside the eager definition-time merge loop, which throws immediately when cva() is called. For compoundVariants, though, tracing the code suggests the throw actually happens later, on component call: compoundVariants is read live inside component(props), and a hole there flows through for (const key in compound) (a no-op for undefined, so matched stays true) into pushClassProps(out, compound), which reads compound.class on undefined. That's a call-time TypeError, not a definition-time one. I wasn't able to execute a repro to confirm this (the sandbox required approval for npx/pnpm that wasn't available here), so please double check the wording against an actual run — worth a quick correction if confirmed, since it's just PR-body prose, not code.
  2. The fast path's !authored.variants / !compounds checks treat an explicit empty variants: {} or compoundVariants: [] as "has variants/compounds" (both are truthy), so those components fall through to the general path even though their output is identical to a plain component. Not a correctness issue, just a missed case for the optimization — not worth changing unless you want to squeeze that case too.

Nothing in AGENTS.md/REVIEW.md/the size-limit bump/whats-new.mdx looks inaccurate or inconsistent with the code (the doc's speed-up percentages are all conservative rounds of the benchmark table, never overstated). I was not able to run pnpm test, the build, or a manual repro script in this environment (npx/pnpm invocations required approval that wasn't grantable here), so this review is based on manual code tracing rather than an executed test/build run — worth a normal CI pass before merge as usual.

joe-bell and others added 7 commits September 13, 2026 13:14
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>
@joe-bell
joe-bell marked this pull request as ready for review September 13, 2026 13:17
@joe-bell
joe-bell merged commit 276f722 into main Sep 13, 2026
11 checks passed
@joe-bell
joe-bell deleted the perf-cva-runtime 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