Skip to content

Size cuts for fetched components, with the repaint rule of #4139 - #4140

Merged
mattgperry merged 17 commits into
mainfrom
claude/project-thread-mwv1t7
Dec 4, 2026
Merged

mattgperry merged 17 commits into
mainfrom
claude/project-thread-mwv1t7

Conversation

@mattgperry

@mattgperry mattgperry commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Requested by Matt · project thread

Size cuts for motion components that fetch their layout and drag code. motion.div from motion/react has a first load of 32.74 kB gzip, down from 33.49 kB on main. That figure includes the repaint rule of #4139, which adds about 0.96 kB with the fixes from review.

What the diff holds

The commits listed under "Changes", rebased on main at 783bbf1 on 4 December. Main now holds #4141, #4125 and #4136. Commit c68ea7e is the diff of #4139, and #4139 is closed in favour of this. #4105 is not in this pull request.

The repaint rule, measured

On main, the transforms of six or more elements that also animate a style on the main thread move to the main thread, on every device. Commit c68ea7e replaces that with the rule of #4139:

  • Up to 100 repainting elements on a touch device (pointer: coarse), and 2,000 on a computer, every transform stays on the compositor.
  • Past that limit, all WAAPI transforms run on the main thread. While they do, each frame is timed. If frames run more than 1.25 times the display interval and other scripts take more than a quarter of a frame, the transforms return to the compositor. They come back after 30 calm frames, and that wait doubles each time.
  • On a touch device with more than 500 repainting elements the block must be longer than a frame, and with more than 1,000 the transforms stay on the main thread.

Results, in fps. Elements animate a transform and a style that repaints. Median of six shuffled rounds, no trace. They are from the head before the rebase of 4 December. A run of the rebased head is queued.

Device class Elements 14.14.0 This pull request
Low power 200 41.8 63.5
Low power 500 22.1 63.5
High power 1,000 111.7 118.9
  • With a script that blocks the main thread each frame: equal to 14.14.0 or above it on both device classes. The largest gap below 14.14.0 in any cell is 1.5 fps, inside the spread of the rounds.
  • Up to 100 elements on Low power: the rule does not act, and the result is equal to 14.14.0 or above it.
  • Known limit: on High power with 3,000 or more elements and a block of about one frame, the rule does not see the block: 74 fps against 90 for CSS animations at 3,000 elements.

Sizes

Gzip kB from node dev/inc/size-scenarios.mjs. The 14.x column is main before #4141. The main column is 783bbf1. The first load is the entry chunk and the chunks it imports statically, built with Rollup and terser.

Entry 14.x main This branch
motion.div, first load 47.04 33.49 32.74
motion.div, all chunks 47.04 50.45 50.25
motion.div + AnimatePresence, first load 35.37 34.86
Reorder, first load 42.37 37.18
m + LazyMotion + domAnimation 31.34 31.68 31.57
m + LazyMotion + domMax 47.21 47.60 47.48
animate 26.52 26.63 27.47
motion.div, esbuild one file, motion/react 49.22 53.76 53.68
motion.div, esbuild one file, motion/react-sync (new) 49.53

Changes

  • Drag code out of Reorder's first load (0d5abbc). A map of drag controls shared a file with the drag controls class. Reorder read the map, so the class and the pan gesture came with it. The map has its own file.
  • Layout measuring logic out of the first load (1e2600b). A fetching component rendered all of MeasureLayout from its first render. It now renders a shell with no logic, and each lifecycle method runs the method that arrives with the layout feature.
  • Exit feature with AnimatePresence (4a6fb21). An exit only runs under AnimatePresence. PresenceChild loads the exit feature, and so does the public PresenceContext for custom presence components. Bundled components and the LazyMotion bundles still include it.
  • motion/react-sync (f1136be). The same exports as motion/react, with components that bundle their features and fetch nothing. It is for builds that write one file. There, esbuild wraps each module that the fetched code shares with the rest of the file in a lazy initialiser, which costs about 4 kB. The name is provisional.
  • Playback controls out of bundles that hand out none (228be6d). A component starts animations and hands out no controls for them. supportPlayback() adds pause, seek, speed and timeline attach to the shared animation classes. The code that gives an animation to its caller calls it: animate(), animateValue(), animateSingleValue(), layout animations, timelines, and the animateTarget() and animateMotionValue() exports. The code that retimes a running WAAPI transform animation moved with it.
  • The repaint rule of Repaint rule: count repainting elements and keep transforms on the compositor when the main thread is blocked #4139 (c68ea7e). See "The repaint rule, measured" above. With the review fixes: about +0.75 kB on the motion.div first load against the six-element rule of main, about +0.96 kB against no rule, and +0.84 kB on animate. Most of it is the check for a blocked main thread (about 0.42 kB).
  • Fixes from review (3c547d1, 556348b, 8e2c3ec), and the size budgets for the new base (a13b523). See "Review" below.
  • The layout shell forgets its component on unmount, as the base does since 6485185 (part of 1e2600b). A component that stopped using layout before the layout code arrived was otherwise set up for layout when the code came.

What changes for users

  • Transforms move to the main thread only past 100 repainting elements on a touch device and 2,000 on a computer, where main moves them from six. See "The repaint rule, measured" above.
  • AnimatePresence alone is 3.80 kB (was 3.03 kB), because it carries the exit feature. With motion.div the pair is smaller.
  • An inspector behind __MOTION_INSPECT__ can pause and seek the animations of components on development builds only. On a production build with only components it still gets every record, but those animations have no pause(), time or speed.
  • Fixed: AnimatePresence never removed a fetching motion.div with layout whose layout code had not arrived, or one with only drag.

Tests

In a cloud container, on this head (a13b523), after the rebase:

  • Jest: motion-dom 1,365 pass. framer-motion client 1,409 pass, server rendering 66 pass. motion 119 pass. motion-utils 38 pass.
  • Typecheck of all packages: pass.
  • node dev/inc/size-scenarios.mjs --check and bundlesize: pass.
  • CircleCI passed every job on the head before the rebase: Jest (test), Playwright (test-html), Cypress on React 18 (test-react) and on React 19 (test-react-19), and measure. Its run on this head is the check to read.

Size limits this pull request raises, and why:

  • animate bundlesize limit, 26.95 to 27.55 kB: the repaint rule is in every bundle that animates a transform with WAAPI.
  • Remix bundlesize limit, 52.8 to 53.65 kB: the rule again.
  • AnimatePresence size budget, 3,150 to 3,950 bytes: it now carries the exit feature, which motion.div no longer does.

CircleCI found failures on the way:

  • A rule test read a busy machine as a blocked main thread (ccbeeae, test only).
  • Two speed tests read two clocks at two moments, and a layer count test met animations that earlier tests had left running (40d0370, test only). The layer count test also failed 2 runs in 3 on the code of Repaint rule: count repainting elements and keep transforms on the compositor when the main thread is blocked #4139 alone.
  • A fetched feature that arrived between a component's commit and its effects was never mounted. That was a fault of the base. The base fixes it in 3ecde78, with the test from here that forces the order.
  • A test from main, delay-sleep.test.ts, expected a delay of 0.5 seconds to still run after five frames. On CircleCI those frames took 529 ms. The delay is now 10 seconds (eab26e2, test only). This pull request does not change the code under that test.

Not run: WebKit, Firefox and the Next.js tests. The device results under "The repaint rule, measured" are from the head before the rebase. Main has gained #4125 and #4136 since, so a run of this head is queued.

Review

Two independent reviews at high effort, one of packages/motion-dom and one of the rest, on eab26e2. Each then reviewed the fixes. Neither found a blocker. They found these faults, each fixed with a test that failed first:

  • A transform on a native scroll timeline was held on the main thread past the repaint limit, and could not return to its timeline. It is now never held. Two related cases of a transform animated directly are fixed with it (3c547d1).
  • A held transform at a speed of 0 returned to the compositor at its start, not where it was held (3c547d1).
  • A transform that ended while it was held stayed in memory, with its element, until the page stopped repainting (3c547d1). The first fix missed a transform whose whole animation was stopped, and a transform that was held before it joined a scroll timeline was built one extra time when the hold ended (8e2c3ec).
  • An m component with layout, beside a motion component that had fetched the layout code, got a projection node and nothing to drive it. On main it took part in layout animations. The layout shell is now loaded for every component, as MeasureLayout was (556348b).
  • That fix gave an app two MeasureLayout components: the shell, and the full one from domMax or a bundled component. A mounted component could change from one to the other, which remounted it and lost one layout animation. A component now keeps the MeasureLayout of its first render (8e2c3ec).
  • The exit feature called register on a presence context without the check it had before it moved (556348b).

Left as they are:

  • An inspector that calls a playback control on an animation a component started gets no control in a production build that only uses components. That is the trade-off of the playback split, and it holds in development builds.
  • motion/react-sync has no explicit m export for editor auto-import, as motion/react has. The entry's name is not final.
  • A transform animated directly that finished while it was held, and is then played again, stays on the main thread until it finishes.
  • Not from this pull request: on main, an animation on the shared JavaScript clock set to a speed of 0 and then back to 1 does not move again. The review found it while it checked the speed fix. It needs its own pull request.

New tests: the first load has no projection-driving code, no exit feature and no playback code. Reorder has no drag modules. Each route that hands out an animation gives it controls, a component animation has none, and an inspector gets them. A transform group runs and finishes without playback. The layout shell drives a node that arrives late. A one-file build from motion/react-sync has no lazy wrappers. The rule has the tests of #4139: the limits for touch and computer, the hold and the return, the blocked-thread check, and a directly animated transform.

https://claude.ai/code/session_016xzo3aG8ZEbuWJW9bSLjET


Generated by Claude Code

mattgperry commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator Author

This comment no longer applies. The failing Jest test came with #4139, and #4139 is out of this pull request since head 942d272.


Generated by Claude Code

@mattgperry
mattgperry force-pushed the claude/project-thread-mwv1t7 branch from 70bfcc4 to 942d272 Compare December 3, 2026 12:34
@mattgperry mattgperry changed the title Take 1.5 kB off the motion.div first load and 6 kB off Reorder's Take 1.7 kB off the motion.div first load and 6 kB off Reorder's Dec 3, 2026

Copy link
Copy Markdown
Collaborator Author

CircleCI test fails on 942d272 in one Jest test: timeline › playTo() plays backwards to an earlier label (timeline.test.ts:490, state is finished where paused is expected).

This is not from this pull request. The test plays a 100 ms timeline backwards for 50 ms in real time. When one frame is longer than the rest of the timeline, the timeline finishes before playTo() pauses it at the label. A test that blocks the main thread for 130 ms after playTo() gives the same result on async-features-beta (b1508f2) and on this head. Without the block the test passes here 6 of 6 times, and it passed in CircleCI on the earlier head.

So the failure is a slow frame on the CI machine, and behind it is a fault in Timeline.playTo() on the base branch. That fix belongs in a pull request of its own. A push here will run the job again.


Generated by Claude Code

@mattgperry
mattgperry force-pushed the claude/project-thread-mwv1t7 branch from 942d272 to b772a5b Compare December 3, 2026 18:20
@mattgperry mattgperry changed the title Take 1.7 kB off the motion.div first load and 6 kB off Reorder's Size cuts for fetched components, with the repaint rule of #4139 Dec 3, 2026
@mattgperry
mattgperry force-pushed the claude/project-thread-mwv1t7 branch 3 times, most recently from 85b3736 to 941a26f Compare December 3, 2026 19:34
Base automatically changed from async-features-beta to main December 3, 2026 20:08
@mattgperry
mattgperry marked this pull request as ready for review December 4, 2026 02:59
claude added 17 commits December 4, 2026 04:33
Reorder.Board looks up an element's drag controls in a map that was
declared in the file of the class that makes them. A bundler keeps a
module whole, so the class, PanSession and the projection geometry they
use were in the first chunk of every page with Reorder.

The map now has its own file. First load with Reorder: 42.96 kB to
38.49 kB gzip.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016xzo3aG8ZEbuWJW9bSLjET
A component that fetches the layout feature rendered all of MeasureLayout
from its first render, so the code that drives a projection node sat in the
first chunk. It now renders a shell with no logic of its own: each lifecycle
method runs the method of the same name that arrives with the feature.
Components that bundle their features are unchanged.

Also fixes an exit that never finished. An async component with layout whose
feature had not arrived, or with only drag, told AnimatePresence it could be
removed before AnimatePresence had noted it as exiting, so the call was lost
and the element stayed in the DOM.

motion.div first load 34.08 -> 33.64 kB gzip.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016xzo3aG8ZEbuWJW9bSLjET
An exit animation only runs for a component that something keeps mounted
after React would remove it. Components that fetch their features no longer
bundle the exit feature: PresenceChild loads it for every motion component
when it first renders. The PresenceContext that apps import loads it too,
for a component that provides the context in place of AnimatePresence.
Components that bundle their features, and the LazyMotion feature bundles,
still include it.

The feature now registers with its presence once the component has an
animation state, so a component with the exit feature and no animation
feature doesn't hold up its presence.

The layout feature holds the MeasureLayout class and not its prototype.
Reading the prototype as the module loaded stopped a bundler dropping the
feature from a flattened bundle that doesn't use it.

motion.div first load 33.64 -> 33.45 kB gzip.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016xzo3aG8ZEbuWJW9bSLjET
The branch combines the async features beta with four open performance
changes, each of which set its own budgets. Also measure the whole React
API through a namespace import: with a fetched chunk that imports from the
first load, Rollup emitted `export *` as a second file that repeats every
export, which added 2.3 kB that no app pays.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016xzo3aG8ZEbuWJW9bSLjET
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016xzo3aG8ZEbuWJW9bSLjET
A bundler that can't fetch code later puts the layout and drag code that
motion/react fetches into its one file. esbuild also wraps every module
that code shares with the rest of the file in a lazy initialiser, for any
import from motion/react.

motion/react-sync has the same exports, with motion components that bundle
their features, so there's nothing to fetch or wrap. In an esbuild bundle
without splitting, motion.div is 50.96 kB gzip (55.09 from motion/react),
and m with LazyMotion and domAnimation is 34.04 kB (36.33).

The entry name is provisional.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016xzo3aG8ZEbuWJW9bSLjET
A component starts animations and hands out no controls for them, so
the code to pause, seek, change the speed of and attach a timeline to an
animation doesn't need to be in a bundle with only components.

supportPlayback() adds those controls to the shared animation classes.
It's called by the code that gives an animation to its caller:
animate(), animateValue(), animateSingleValue(), layout animations,
timelines, and the animateTarget() and animateMotionValue() exports. The
transform group's retiming of a running WAAPI animation moves with them.

An inspector behind __MOTION_INSPECT__ controls the animations of
components too. It gets the controls on development builds.

motion.div first load: 33.45 kB to 32.57 kB gzip. animate(): +0.07 kB.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016xzo3aG8ZEbuWJW9bSLjET
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016xzo3aG8ZEbuWJW9bSLjET
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016xzo3aG8ZEbuWJW9bSLjET
Brings the repaint rule of #4139 onto the size commits. Up to 100
repainting elements on a touch device, and 2,000 on a computer, every
transform stays on the compositor. Past that, transforms run on the
main thread, and return to the compositor while other scripts block it.

Replaces the rule that moved the transforms of six or more repainting
elements to the main thread on every device.

Sizes, gzip: motion.div first load 32.36 kB (31.68 kB with the old
rule), animate 27.30 kB.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016xzo3aG8ZEbuWJW9bSLjET
On a CI machine busy with other suites, frames ran long after the test
stopped its block, and the late probe timer read as other scripts. The
transforms then stayed on the compositor past the 120 frames the test
waits. The timer is held from the moment the block ends, as it is for
the tests that never block.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016xzo3aG8ZEbuWJW9bSLjET
The speed tests compared a WAAPI animation with the animation that
drives it in two reads of the clock. A stall between the reads counts
twice at double speed, and CI had one of 25ms. Both are now read at one
moment.

The layer count test took the number of compositor layers before it
started. Animations that earlier tests left running each held a layer
until they finished, and some finished during the test. Each test's
animations are now stopped before the next.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016xzo3aG8ZEbuWJW9bSLjET
Two tests of values in their delay read the value after a few frames and
expect the delay of 0.5 seconds to still be running. On CircleCI the five
frames of the first test took 529 ms, so the animation had started and
the value was 29, not 0. A stall of 450 ms in the test gives the same
failure here.

The delay is now 10 seconds, and each test stops its animation once it
has read the value. Test only.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016xzo3aG8ZEbuWJW9bSLjET
Three faults of the rule, found in review:

A transform on a native scroll timeline was held on the main thread like
any other while too many elements repainted. To hold it, it's made to
follow its timeline from JS, and it can't go back. Only the compositor
moves it with the scroll, so it's now never held. A `transform` animated
directly that was held before its timeline was attached joins the
timeline on the compositor, and one that follows a timeline from JS is
no longer started again on a clock when the hold ends.

A held `transform` at a speed of 0 returned to the compositor at its
start. It's running, so it was given a start time, and at a rate of 0
that leaves it at 0. It's now set to where it's held.

A transform that ended while it was held stayed in the set of held
transforms, with its element, until the page stopped repainting. It's
now forgotten when its animation finishes or stops.

This is 62 bytes gzip on `animate`, so its bundlesize limit goes from
27.4 to 27.45 kB, and the Remix limit from 53.45 to 53.5 kB.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016xzo3aG8ZEbuWJW9bSLjET
Features are shared by every component on the page. The components that
fetch layout code used to load their MeasureLayout for all of them, so an
`m` component with `layout` took part once the code had arrived. Since
the shell replaced it, the fetched code brought a projection node for an
`m` component and nothing to drive it from React: no snapshot before a
layout change, no layout group, no promotion.

The shell is now loaded for every component, as MeasureLayout was.

The exit feature also checks that a presence context has `register`
before it calls it, as it did before it moved.

Both found in review.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016xzo3aG8ZEbuWJW9bSLjET
From the second pass of the review.

A component now keeps the MeasureLayout it first renders. Features are
shared by every component on the page, and since the last commit the
components that fetch layout code load the shell for all of them, where
`LazyMotion` with `domMax` and the components that bundle their features
load the bundled one. In an app with both, a mounted `m` or bundled
component was given the other on a later render. That mounts it again,
and a mount takes no snapshot, so that layout change wasn't animated.

A transform group that was held before it joined a scroll timeline is
forgotten when it joins, so the end of the hold doesn't build its
animation again. A held `transform` is forgotten when its whole
animation is stopped, not only when it is stopped alone.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016xzo3aG8ZEbuWJW9bSLjET
Main now holds #4125 and #4136, which add about 0.29 kB to a motion
component and 0.11 kB to animate. The bundlesize limits for animate and
Remix go to 27.55 kB and 53.65 kB for the same reason.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016xzo3aG8ZEbuWJW9bSLjET
@mattgperry
mattgperry force-pushed the claude/project-thread-mwv1t7 branch from 845ee7c to a13b523 Compare December 4, 2026 04:40
@mattgperry
mattgperry merged commit 8ffcad8 into main Dec 4, 2026
2 of 6 checks passed
@mattgperry
mattgperry deleted the claude/project-thread-mwv1t7 branch December 4, 2026 05:02
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.

2 participants