Skip to content

Avoid parsing the join HTML three times - #4508

Open
etareduction wants to merge 2 commits into
phoenixframework:mainfrom
Forge-of-Apps:skip-redundant-join-parses
Open

etareduction wants to merge 2 commits into
phoenixframework:mainfrom
Forge-of-Apps:skip-redundant-join-parses

Conversation

@etareduction

Copy link
Copy Markdown

On a LiveView's first join, the client turns the rendered HTML into DOM three times:

  1. maybeRecoverForms sets template.innerHTML = html even when formsForRecovery is empty, which it always is on a first join (getFormsForRecovery returns {} while joinCount === 0), so the template is thrown away unused.
  2. onJoinComplete calls DOM.findPhxChildrenInFragment(html, this.id), which parses the HTML again to look for child LiveViews.
  3. DOMPatch hands the HTML string to morphdom, which parses it a third time.

Without forms to recover and without child LiveViews, only the third parse is needed. This happens on every page load and every live navigation, and on a large page each parse is a noticeable part of the frame that applies the mount.

This PR:

  • returns early from maybeRecoverForms when there are no forms to recover, before creating the template;
  • skips the parse in findPhxChildrenInFragment when the HTML does not contain data-phx-parent-id (case-insensitively), the attribute every nested LiveView is rendered with and that findPhxChildren matches on;
  • collects the forms for recovery into an object without a prototype. Otherwise a form with the id __proto__ replaces the object's prototype instead of becoming a key, and the new empty check would skip recovering it.

Why behavior doesn't change

  • maybeRecoverForms: when formsForRecovery has no keys, the existing filter newForm.id && oldForms[newForm.id] can only match through Object.prototype (ids like constructor), and the code then throws on .getAttribute. In every other case it calls the callback synchronously. Everything it does before that touches only the throwaway template.
  • findPhxChildrenInFragment: the HTML parser takes attribute names from the source, lowercasing ASCII letters and not decoding character references. So if those characters are not in the HTML in some letter case, no element can carry the attribute, and the query returns [].
  • Neither change caches anything: both are early returns at the same call sites, evaluated at the same time as before.

To check this beyond the argument, I ran the jest and e2e suites (Chromium, Firefox, WebKit) against a build where each early return also ran the old code on the same input and threw if the two disagreed. There were no disagreements over 2,004 skipped parses in the e2e suite and 450 in jest, and the e2e suite passed. With the conditions deliberately broken, the same build reported 5 disagreements.

Numbers

A single-file app (based on .github/single-file-samples/main.exs) navigates from a small page to a page of N cards, each about 270 elements. The minified CDN bundle was built from main and from this branch. It ran on a Hetzner CCX33 (8 dedicated EPYC Milan vCPUs, 0% steal), with the app, the browser and the rest of the system pinned to separate cores, using the browsers that Playwright 1.65 installs. Runs alternate in ABBA order, 30 pairs per row. Values are medians. "Parse time" is the time spent in template.innerHTML during the join, "onJoin" the wall time of View.prototype.onJoin.

Browser Page Join Parse time onJoin
Chromium 158 2.0 MiB, 27k elements live navigation 107 → 36 ms 202 → 140 ms
Firefox 159 2.0 MiB, 27k elements live navigation 101 → 34 ms 199 → 134 ms
WebKit 27.2 2.0 MiB, 27k elements live navigation 125 → 40 ms 254 → 165 ms
Chromium 158 610 KiB, 8k elements live navigation 48 → 23 ms 86 → 70 ms
Firefox 159 610 KiB, 8k elements live navigation 42 → 20 ms 78 → 58 ms
WebKit 27.2 610 KiB, 8k elements live navigation 54 → 26 ms 101 → 77 ms
Chromium 158 2.0 MiB, 27k elements page load 91 → 30 ms
Firefox 159 2.0 MiB, 27k elements page load 102 → 33 ms
WebKit 27.2 2.0 MiB, 27k elements page load 118 → 38 ms

The 95% bootstrap interval of every paired difference excludes zero. In Chromium, the long animation frame holding the 2 MiB live navigation's join went from 240 to 176 ms.

Tests

  • dom_test.ts: findPhxChildrenInFragment still finds the children of the given parent, also when the attribute is not lowercase, and does not parse HTML without child LiveViews.
  • view_test.ts: maybeRecoverForms does not parse without forms to recover, and still recovers a form with the id __proto__. A first join without child LiveViews parses its HTML once (three times on main), and a first join still joins a child LiveView found in its HTML.

🤖 Generated with Claude Code

etareduction and others added 2 commits November 13, 2026 21:37
On a view's first join, maybeRecoverForms parsed the rendered HTML into
a template even when there were no forms to recover, and
findPhxChildrenInFragment parsed it again to look for child LiveViews,
before the patch parsed it a third time. On large pages each parse is
a noticeable part of the mount.

Skip form recovery when there are no forms to recover, and only parse
for child LiveViews when the HTML contains the parent id attribute they
are rendered with.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Collect the forms for recovery into an object without a prototype, so
that a form with the id "__proto__" is a key of it: before, it replaced
the object's prototype, left no keys behind and was skipped by the empty
check. Match the parent id attribute case-insensitively, as the HTML
parser lowercases attribute names.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

This branch has not been deployed

No deployments
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