https://www.reddit.com/r/Bard/comments/1jpt9ux/for_google_devs_ai_studio_lag_likely_causes_tldr/
If any Google devs happen to be lurking, just wanted to drop a few notes that might help debug this issue. Here’s what I’ve been seeing:
The Issue:
- Main problem: The UI starts lagging really badly as the chat gets longer. It doesn’t feel linear - more like exponential slowdown.
- What happens: Typing gets super delayed (2-3 seconds input lag at first, 10-15 seconds later as the chat keeps growing), and buttons (Send/Run) take a while to respond after clicking.
- What triggers it: Seems tied to the total length of the conversation (user + AI messages over time), not just the size of the current message. Brand new chats feel fine.
- Frontend issue?: The lag kicks in before a message is even sent (while typing) and happens no matter what model is selected, which makes it look like a frontend bottleneck.
- Cross-platform: Reproducible on Windows/Mac across Chrome, Brave, Firefox, and on mobile (Safari on iOS, Chrome/Brave on Android).
What Might Be Causing It:
a) DOM Bloat (Most Likely Primary Cause):
Chrome dev tools show that DOM node count starts around 2-3k in a fresh chat, but blows up to 100k+, even 300k+ as the chat grows. There doesn’t seem to be a limit.
The more DOM nodes there are, the slower everything gets. Strong inverse correlation.
Typing triggers what looks like massive layout/repaint work across the entire DOM.
CPU usage also shoots up - It hits 100% on a decent machine just from typing in a long chat. Here’s a screenshot from Brave dev tools showing it: Imgur: The magic of the Internet.
My guess is the whole chat history is being rendered at once with no virtualization. That’s a lot of content for the browser to keep up with.
I think virtual scrolling is worth trying here.
b) Frequent countTokens Calls (Likely Contributing Factor):
I’ve noticed tons of countTokens (or similar) network requests firing constantly while typing - often looking like one per keypress.
While likely not the root cause of the exponential slowdown (which points to DOM), this constant network chatter during input definitely seems to contribute to the perceived input lag and sluggishness. Even if async, any latency or processing delay on these frequent calls can make the typing experience feel stuttery or unresponsive.
This might be exacerbating the slowdown caused by the DOM issues, especially as the main thread gets busier.
Could debouncing these calls (e.g., fire only after typing pauses for 250-500ms) and ensuring they are truly non-blocking help?
TL;DR:
Massive DOM size from rendering the full chat history is almost certainly the main issue causing the exponential slowdown (virtualization as a possible fix?). However, the very frequent token-counting network requests during typing likely exacerbate the problem and contribute significantly to the input lag.