Skip to content

Navigation costs: SvelteKit's link scan and page walks in the DOM layer - #13

Open
theetherGit wants to merge 5 commits into
mainfrom
nav-cost
Open

theetherGit wants to merge 5 commits into
mainfrom
nav-cost

Conversation

@theetherGit

Copy link
Copy Markdown
Contributor

Navigation in SvelteKit apps gets cheaper in the DOM layer.

Headline (Tracker, 1,000 issues, session nav-1, main against this branch, interleaved, 1 warm-up and 3 runs, Stage Manager off): the turn that renders the Board is 5.5 ms shorter, and Issues' 3.2 ms. SvelteKit's look at each link after a navigation goes from 16.3 to 7.3 µs a link.

The full report is compare/results/nav-cost.md, generated by make_nav_report.py.

What it does

After every navigation SvelteKit walks the whole page twice. One walk is for a, to set up link preloading. For each link it reads href, makes a new URL of it, and calls getAttribute five times on every ancestor up to <html>. The other walk is for [autofocus]. Each change below is its own commit:

commit what changes before after
b887ad3 drive gets a gc: step: a cycle collection before a timed step, so the before and after numbers use the same tool
dc3c802 a link's href is resolved once per attribute value and page address 4.9 µs a link 0.47 µs
64706ea a selector's test is built once. A single tag, attribute, id or class gets a test of its own; any other compound is matched directly querySelectorAll('a') on the Board 1,775 µs, [autofocus] 2,475 µs 800 µs, 750 µs
6fec75a url.js: searchParams is made on first use; the raw query and the no-authority flag are private fields new URL(href, base) 4.27 µs 0.87 µs

Board repeat visit, headless, with a collection forced before each step: 76 → 73 → 70 → 69 ms across the three fixes.

One behaviour change: searchParams is no longer an own property of a URL. That matches browsers and Node, and it's in the CHANGELOG.

In a window

main branch Δ
Board: the turn that renders it 109.2 ms 103.7 −5.5
Issues: the turn that renders it 44.4 41.2 −3.2
Insights: the turn that renders it 39.6 40.5 +0.9
Settings: the turn that renders it 23.8 23.3 −0.5
idle 20 s: frames, wake-ups 0, 34 0, 32 same

Insights' mark is 7.6 ms later on the branch, while its render turn is the same. The difference is in where the click lands in the frame. SvelteKit follows a link from a requestAnimationFrame callback, which waits for the next vsync. While the nav item's hover transition draws, frames come every 13 ms, so a mark can move by up to a frame depending on where the click falls in that cycle. Headless, with no frames, Insights' first visit is 1 ms quicker.

Found on the way, not changed here

  • A screen's first visit pays for the process growing. The Board's first visit costs ~50 ms more than a repeat, on old main too, and the cost is spread evenly over its 300 cards. Making and dropping 20,000 of the renderer's nodes first takes all of it away. It is not cn(), Svelte's templates, Bits UI, the app's data, or QuickJS's collector. That's the next job.
  • The split build's first visits trail a single bundle by only 1–1.5 ms, and not at all on the Board. No turn waits for a route's stylesheet, and the route's chunk is read in the same turn that renders it.
  • The 4.7 ms the Board trailed old main by (in preload-fix.md, with the scan removed) was the [autofocus] walk.

Tests

  • dom.test.js:
    • the href cache follows a changed attribute and a navigation
    • each built-once test gives the same answer as the general matcher, checked through :not(:not(…))
  • url.test.js:
    • searchParams made after a write to search or href, and writes after it
    • still matches Node's URL on 22 and 26
  • pnpm test (151 QuickJS host tests) and the V8 host tests (145) pass locally.

🤖 Generated with Claude Code

theetherGit and others added 5 commits November 27, 2026 00:47
A cycle collection, with the DROPs it queues applied. Put before a timed
step, it keeps a collection from landing in one run and not the next:
without it, the Tracker's Board visit varies from 78 to 117 ms headless;
with it, from 80 to 87. The measurements for this branch use it, before
and after.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SvelteKit reads every link's `href` after each navigation, to set up
preloading, and the getter built a whole URL each time to return its
string. The resolved string is now kept, keyed by the attribute and the
page's address, so a changed attribute or a navigation gives a fresh
answer (tested both ways).

Headless, the Tracker at 1,000 issues, a cycle collection before each
step, medians of 7:

| | before | after |
|---|---:|---:|
| `href`, per link on the Board | 4.9 µs | 0.47 µs |
| SvelteKit's link loop, per link | 16.3 µs | 11.95 µs |
| a repeat visit to the Board | 76 ms | 73 ms |
| a first visit to the Board | 136 ms | 137 ms |

A first visit gains nothing: the page's address is new, so every link
misses. Repeat visits are where it pays.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Walks of the page went through the general matcher for every element:
the parsed selector looked up, each alternative tried, a closure made per
element. SvelteKit walks the whole page twice after every navigation,
for `a` (its link scan; the tag index gives up above 64 elements) and
for `[autofocus]`. A selector's test is now built once: one tag, one
attribute that must be there, one id or one class gets a test of its
own, any other compound without combinators is matched directly, and the
rest as before. The tests check each against the general matcher.

Headless, the Tracker at 1,000 issues, a cycle collection before each
step, medians of 7:

| | before | after |
|---|---:|---:|
| `querySelectorAll('a')` on the Board (3,529 elements) | 1,775 µs | 800 µs |
| `querySelector('[autofocus]')` on the Board | 2,475 µs | 750 µs |
| a repeat visit to the Board | 73 ms | 70 ms |
| a first visit to the Board | 137 ms | 134 ms |

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Every `new URL` made a URLSearchParams and wrote two WeakMaps, the raw
query and whether the URL has an authority, even when nothing would read
them. Routers make URLs for their parts: SvelteKit one per link on the
page after every navigation. `searchParams` is now made on first use,
from the query as it is then, and the other two are private fields that
`parse` reads through the class. `searchParams` is no longer an own
property, as it is not in Node. Tested against Node's URL on 22 and 26,
and for writes before and after the params are first read.

Headless, the Tracker at 1,000 issues, a cycle collection before each
step, medians:

| | before | after |
|---|---:|---:|
| `new URL(href, base)`, per link on the Board | 4.27 µs | 0.87 µs |
| SvelteKit's link loop, per link | 12.18 µs | 7.28 µs |
| a repeat visit to the Board (2 rounds of 9) | 70 ms | 69 ms |

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
compare/results/nav-cost.md, from make_nav_report.py: session nav-1,
main against this branch at 1,000 issues (idle, opening an issue,
switching screens), with SvelteKit's link scan piece by piece before and
after, and what a first visit costs. HANDOVER: the job, the growth cost
a first visit pays (the next job) and what is left. CHANGELOG: navigation
is faster, and `searchParams` is no longer an own property of a URL.
README: what navigation costs. CLAUDE.md: `drive`'s `gc:` for timing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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