demo · v139
Streaming vs Batch
promptStreaming() yields tokens as Gemini Nano generates them — the user sees words appearing in real time. prompt() waits for the full response before returning. This demo runs both side-by-side so you can measure the perceived latency difference.
Origin trial / flag
The Prompt API (Gemini Nano in Chrome) requires enabling
chrome://flags/#optimization-guide-on-device-model
and chrome://flags/#prompt-api-for-gemini-nano, plus downloading the on-device model.
If unavailable, the demo shows a simulated streaming response to illustrate the UX difference.
Checking LanguageModel availability…
prompt() (batch) — time to first token
—
promptStreaming() — time to first token
—
prompt() — batch
Output will appear here when complete.
promptStreaming() — streaming
Output will appear token by token.
// Batch: waits for full response
const session = await LanguageModel.create();
const start = performance.now();
const result = await session.prompt(text);
const elapsed = performance.now() - start;
// Streaming: yields chunks as they arrive
const streamStart = performance.now();
let firstChunk = true;
const stream = session.promptStreaming(text);
for await (const chunk of stream) {
if (firstChunk) {
const ttft = performance.now() - streamStart; // time to first token
firstChunk = false;
}
outputEl.textContent += chunk;
}