perf: optimize SSE streaming buffer to avoid quadratic string growth (#1024)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
0396847ae3
commit
e66039162e
1 changed files with 7 additions and 1 deletions
|
|
@ -410,8 +410,14 @@ async function* parseSSE(response: Response): AsyncGenerator<Record<string, unkn
|
|||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
buffer += decoder.decode(value, { stream: true });
|
||||
|
||||
const decoded = decoder.decode(value, { stream: true });
|
||||
// Avoid appending to an empty buffer — assign directly to skip the
|
||||
// string concatenation and its intermediate allocation.
|
||||
buffer = buffer ? buffer + decoded : decoded;
|
||||
|
||||
// Consume all complete SSE messages (delimited by \n\n) so the
|
||||
// buffer only ever holds one partial message between reads.
|
||||
let idx = buffer.indexOf("\n\n");
|
||||
while (idx !== -1) {
|
||||
const chunk = buffer.slice(0, idx);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue