Skip to content

Batch initial element measurement after register - #283

Open
spaansba wants to merge 3 commits into
mainfrom
batch-register-measure
Open

spaansba wants to merge 3 commits into
mainfrom
batch-register-measure

Conversation

@spaansba

Copy link
Copy Markdown
Owner

register() read getBoundingClientRect per element. Vue calls function refs mid-patch, so every registration forced a full layout, which made mounting quadratic. Mounting 10,000 elements on the mass demo: 21.4s → 0.76s.

Elements are now measured in one microtask after the registering task, all reads before any writes. Behavior change: getBounds() and isIntersectingWithViewport are empty/false if read synchronously right after register(). Documented in the registration docs.

…task

Reading the rect inside register() forced a layout per element when a framework
inserts and registers elements in turn (Vue function refs), which is quadratic.
10,000 elements on the mass demo went from 21.4s to 0.76s to mount.
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Deploying foresightjs with  Cloudflare Pages  Cloudflare Pages

Latest commit: a06e5b1
Status: ✅  Deploy successful!
Preview URL: https://d4e24b5e.foresightjs.pages.dev
Branch Preview URL: https://batch-register-measure.foresightjs.pages.dev

View logs

@changeset-bot

changeset-bot Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 5212344

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
js.foresight Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ No critical issues — two documentation gaps worth a look.

Reviewed changes

  • Batched measurement — register() now queues entries in unmeasuredElements and measures them in a single queueMicrotask (measureNewElements), reading every rect before any write.
  • Empty initial bounds — createElementInternal seeds bounds with a zero DOMRectReadOnly and zero expandedRect, and leaves isIntersectingWithViewport false until measurement.
  • Scannable set — register() no longer adds to scannableElements directly; membership now follows the post-measurement updateElementState call.
  • Tests — new deferral test plus await settle() moved ahead of subscription in the bounds-channel tests.
  • Docs/changeset — note added under getBounds() and a patch changeset.

I ran the core suite (174 passed) and tsc --noEmit (clean). The scheduling logic holds up: the unmeasuredElements.size === 0 guard queues exactly one microtask per task, the elementEntries.get(entry.element) !== entry check correctly skips entries unregistered or re-registered before the flush, and in-viewport active elements still land in scannableElements because the isIntersectingWithViewport flip makes updateElementState take the changed path.

ℹ️ elementRegistered payload is now pre-measurement, but the event docs still imply otherwise

The event is emitted synchronously inside registerElement, before the microtask, so e.state.isIntersectingWithViewport is now always false and getElementBounds() returns zero rects for listeners. The devtools overlay and element tab cope because they subscribe to state/bounds, but the public contract in packages/docs/docs/_partials/_events.mdx (the elementRegistered section) doesn't mention it, and the changeset only talks about getBounds()/isIntersectingWithViewport generically. Worth a sentence there so event consumers know to read the state after the registering task.

Technical details
# Document the pre-measurement `elementRegistered` payload

## Affected sites
- `packages/docs/docs/_partials/_events.mdx` — `elementRegistered` section describes `state: ForesightElementState` with no note that it is captured before measurement.
- `packages/js.foresight/src/managers/ForesightManager.ts:292` — event emitted before `measureNewElements` runs.

## Required outcome
- Event consumers can tell from the docs that `state.isIntersectingWithViewport` is `false` and bounds are empty at `elementRegistered` time, and that they should use `subscribe`/`subscribeToBounds` (or read `registeredElements` later) for the measured values.

## Open questions for the human
- Is a patch bump still the right call given the event payload change, or should this be a minor?

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏


Returns the current immutable `ElementBounds` snapshot for this element: its measured `originalRect` and the hitSlop-expanded `expandedRect` used for interaction detection.

Elements are measured right after the task that registered them, in one batch, so `getBounds()` returns empty rects (and `isIntersectingWithViewport` is `false`) if read synchronously after `register()`. Use `subscribeToBounds` or `subscribe` to get the first measurement.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

subscribe (the state channel) won't fire for an element that is offscreen at first measurement: isIntersectingWithViewport stays false, so updateElementState early-returns and no state subscriber is notified. Only subscribeToBounds reliably delivers the first measurement. Suggest narrowing this to subscribeToBounds (or noting the caveat).

Every registered element rendered a full row (~100 DOM nodes with three shadow
roots). With 10,000 elements the page held over a million nodes and the browser
froze. Only visible rows are rendered now.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes

Reviewed the devtools list virtualization commit since the prior pullfrog review; the core measurement change is unchanged.

  • Virtualized elements tab — replaced the per-section repeat render with a single virtualize({ scroller: true }) list, so only rows near the viewport are in the DOM.
  • Flattened row model — introduced a ListRow union (header / element), sectionRows, toggleSection, and a keyed renderRow, dropping the .section wrapper in favour of one absolutely positioned row per header or element.
  • Dependency — added @lit-labs/virtualizer (^2.1.1) to the devtools package and lockfile; it bundles into the existing single dist/index.mjs.
  • Changeset — added a patch changeset for the devtools performance work.

I verified the mechanics rather than just the shape: the virtualize directive hosts the Virtualizer on .list itself (no lit-virtualizer element needed), _applyVirtualizerStyles supplies position: relative, contain: size layout, and overflow: auto inline, and FlowLayout applies only translate() to children, so the new single-element { width: 100% } rule is what gives rows their width. .list { height: 100% } resolves through tab-content's .content and the absolutely positioned .tab-content > * chain, and the inactive separator survives all: unset by specificity. Rendering the header as a virtualized row rather than a wrapper also means collapse toggles still re-render through the rangeChanged path since every render produces fresh row objects. The devtools package typechecks clean after building core, builds with tsdown (89.5 kB ESM), and its suite passes; prettier and eslint are clean on the changed files.

Pullfrog  | Fix it ➔ | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ No new issues in the revert — the PR now ships only the core measurement change. Not an approval only because the earlier docs thread is still open.

Reviewed changes

Reviewed the follow-up commit since the prior pullfrog review; the core batched-measurement change is unchanged.

  • Devtools virtualization reverted — 5212344 removes the elements-tab virtualize rewrite, the @lit-labs/virtualizer dependency and its lockfile entries, plus the devtools performance changeset. packages/js.foresight-devtools and pnpm-lock.yaml are now byte-identical to main.
  • Scope — the PR diff is back to five files (+67/−15): the manager microtask, the empty initial bounds, the deferral test, the registration docs note, and the js.foresight patch changeset.

The earlier inline concern on _registration-options.mdx:221 (state subscribe not firing for an element that is offscreen at first measurement) is untouched by this commit, so that thread stays open.

Pullfrog  | Fix it ➔ | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant