Skip to content

perf: smart pre-navigation — skip redundant nav + remove 2s wait#507

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

perf: smart pre-navigation — skip redundant nav + remove 2s wait#507
jackwener merged 3 commits intomainfrom
worktree-perf-smart-prenav

Conversation

@jackwener
Copy link
Copy Markdown
Owner

Summary

  • Skip pre-navigation entirely when the browser is already on the target domain (saves ~2s per consecutive same-site command)
  • 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
  • Add getCurrentUrl() to IPage, Page, and CDPPage for domain detection

Performance Impact

  • Same-site consecutive commands: ~2-3s faster (skip navigation + sleep entirely)
  • First command / cross-site: ~1-2s faster (remove redundant wait(2), rely on existing waitForDomStable)
  • Non-browser commands: no change

Test plan

  • npx tsc --noEmit passes
  • All 496 unit tests pass
  • getCurrentUrl() returns null gracefully on error (no new failure modes)
  • isAlreadyOnDomain() falls back to false on any exception (safe default)

…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 cf99c61 into main Mar 27, 2026
25 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