From a0d6c209c3027a8f453001915eadc798fb39c98a Mon Sep 17 00:00:00 2001 From: Classic298 <27028174+Classic298@users.noreply.github.com> Date: Sun, 1 Mar 2026 19:37:10 +0100 Subject: [PATCH] perf: fast-path token comparison in CodeBlock (#22101) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit During streaming, every token change triggers a full JSON.stringify comparison on the code block token object. Add an O(1) fast-path check on token.text and token.raw — the fields that actually change during streaming — before falling back to the expensive JSON.stringify comparison for infrequent structural changes. --- src/lib/components/chat/Messages/CodeBlock.svelte | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib/components/chat/Messages/CodeBlock.svelte b/src/lib/components/chat/Messages/CodeBlock.svelte index ebd9892bc..a058b1545 100644 --- a/src/lib/components/chat/Messages/CodeBlock.svelte +++ b/src/lib/components/chat/Messages/CodeBlock.svelte @@ -364,7 +364,9 @@ }; $: if (token) { - if (JSON.stringify(token) !== JSON.stringify(_token)) { + if (token.text !== _token?.text || token.raw !== _token?.raw) { + _token = token; + } else if (JSON.stringify(token) !== JSON.stringify(_token)) { _token = token; } }