Skip to content

perf: smart pre-navigation, DOM-stable waits, in-memory URL tracking#524

Merged
jackwener merged 3 commits intomainfrom
worktree-perf-smart-prenav
Mar 27, 2026
Merged

perf: smart pre-navigation, DOM-stable waits, in-memory URL tracking#524
jackwener merged 3 commits intomainfrom
worktree-perf-smart-prenav

Conversation

@jackwener
Copy link
Copy Markdown
Owner

Summary

  • Skip redundant navigation: if the page is already on the same domain as preNavUrl, skip the goto() call entirely — avoids unnecessary page reload and DOM-settle round-trip
  • Remove hardcoded wait(2): page.goto() already includes DOM-stable waiting; the extra 2s sleep was pure waste
  • DOM-stable early return for wait(): waits ≥ 1s now check DOM stability first and resolve as soon as the page settles (up to 5s max), rather than always sleeping the full duration
  • In-memory URL tracking: getCurrentUrl() reads this._currentUrl (set on every goto()) instead of issuing a CDP round-trip — zero network overhead

Test plan

  • Run unit + adapter tests: npx vitest run --project unit --project adapter
  • Verify pre-nav skip logic on a site where cookie strategy is used (e.g. weread, bilibili)
  • Verify wait() smart-settle fires early on a slow-loading page

…oded 2s wait

- Add `getCurrentUrl()` to IPage, Page, and CDPPage to check current browser URL
- Skip pre-navigation entirely if the browser is already on the target domain
- Remove the hardcoded `page.wait(2)` after pre-navigation — `page.goto()` already
  includes smart DOM-settle detection via `waitForDomStable`, making the fixed
  2-second sleep redundant
- Saves ~2s per browser command in the common case (consecutive commands on the
  same site), and ~1-2s even on cold navigation
For page.wait(N) where N >= 1 second, use DOM MutationObserver-based
stability detection instead of a fixed sleep. The original wait time
becomes a hard cap, but the call returns as soon as the DOM stops
mutating (500ms quiet period).

This benefits ~200 hardcoded sleep calls across ~40 adapters without
changing any adapter code. A typical page.wait(5) now completes in
<1s when the page is already stable, instead of always waiting 5s.

Short waits (< 1s) are kept as fixed sleeps — these are typically
UI animation delays or anti-bot throttling where DOM-ready is irrelevant.
Replace the sendCommand('exec', 'window.location.href') call with a
local _lastUrl field set during goto(). This eliminates a daemon HTTP
round-trip for the domain check, making isAlreadyOnDomain() zero-cost.

On fresh tabs (about:blank), _lastUrl is null so we correctly fall
through to navigation — no special-casing needed.
@jackwener jackwener merged commit 3834292 into main Mar 27, 2026
50 checks passed
@jackwener jackwener deleted the worktree-perf-smart-prenav branch March 27, 2026 16:46
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