CDP Performance Optimization - Practical Analysis
Date: 2026-01-07 Context: Browser automation for Twitter replies was slow - analyzing why and how to fix
Problem Identified
When running multiple browser operations via local-browser.js:
./local-browser.js navigate https://x.com
./local-browser.js screenshot /tmp/x.png
./local-browser.js click "Sign in"
./local-browser.js type "hello"
Each command creates a new process that:
- Creates new CDPClient instance
- Opens WebSocket connection to Chrome
- Enables domains (Page, DOM, Runtime)
- Executes ONE operation
- Disconnects WebSocket
- Process exits
Total overhead per command: ~500-800ms just for setup/teardown
For 12 operations in the Twitter workflow = 6-10 seconds wasted on connection overhead alone.
Root Cause
CDP is designed for persistent connections - the WebSocket stays open and you send multiple commands through it. But my CLI tool pattern (./local-browser.js <cmd>) forces new connections each time.
Solutions
Option 1: Persistent CDP Service (Best)
Run a daemon that maintains persistent CDP connection:
- Commands sent via Unix socket or HTTP
- Single WebSocket connection reused
- Domains enabled once at startup
- Near-zero overhead per command
Option 2: Batch Command Mode
Add a batch mode to process multiple commands:
./local-browser.js batch << EOF
navigate https://x.com
sleep 2000
click "Sign in"
type "hello"
screenshot /tmp/result.png
EOF
Single connection, multiple operations.
Option 3: Interactive REPL Mode
./local-browser.js repl
> navigate https://x.com
> click "Sign in"
> type "hello"
Connection persists until exit.
Other Optimizations
-
Smart waits vs fixed sleeps
- Use
Page.loadEventFiredinstead ofsleep(2000) - Use
DOM.querySelectorpolling instead of arbitrary waits
- Use
-
Reduce verification screenshots
- Only screenshot on errors or final results
- Trust that operations succeeded unless they throw
-
Parallel operations where safe
- Multiple evals can run in parallel
- But sequence matters for UI operations
Implementation Priority
- Batch mode - Quick win, solve immediate problem
- Persistent service - Better long-term but more work
- Smart waits - Incremental improvement
Key Insight
The CLI-per-command pattern is fundamentally wrong for CDP.
CDP's architecture assumes you maintain a connection and send commands through it. Spawning a new process for each command defeats the purpose of the persistent WebSocket design.