Repository navigation
Conversation
…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.
Deploying foresightjs with
|
| Latest commit: |
a06e5b1
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://d4e24b5e.foresightjs.pages.dev |
| Branch Preview URL: | https://batch-register-measure.foresightjs.pages.dev |
🦋 Changeset detectedLatest commit: 5212344 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
There was a problem hiding this comment.
ℹ️ No critical issues — two documentation gaps worth a look.
Reviewed changes
- Batched measurement —
register()now queues entries inunmeasuredElementsand measures them in a singlequeueMicrotask(measureNewElements), reading every rect before any write. - Empty initial bounds —
createElementInternalseedsboundswith a zeroDOMRectReadOnlyand zeroexpandedRect, and leavesisIntersectingWithViewportfalse until measurement. - Scannable set —
register()no longer adds toscannableElementsdirectly; membership now follows the post-measurementupdateElementStatecall. - 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?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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
✅ 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
repeatrender with a singlevirtualize({ scroller: true })list, so only rows near the viewport are in the DOM. - Flattened row model — introduced a
ListRowunion (header/element),sectionRows,toggleSection, and a keyedrenderRow, dropping the.sectionwrapper 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 singledist/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.
deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏
This reverts commit 61ca8a2.
There was a problem hiding this comment.
ℹ️ 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 —
5212344removes the elements-tabvirtualizerewrite, the@lit-labs/virtualizerdependency and its lockfile entries, plus the devtools performance changeset.packages/js.foresight-devtoolsandpnpm-lock.yamlare now byte-identical tomain. - 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.foresightpatch 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.
deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

register()readgetBoundingClientRectper 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()andisIntersectingWithViewportare empty/falseif read synchronously right afterregister(). Documented in the registration docs.