From c7afbec719e1abcd45ff71235679075a6bce4357 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 10 Nov 2026 07:10:30 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Optimize=20CodeBlock=20rend?= =?UTF-8?q?er=20performance?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces nested .map().join() array methods with useMemo and manual string concatenation loops inside CodeBlock's `shown` state calculation to reduce unnecessary allocations on a hot path. Co-authored-by: bobdivx <6737167+bobdivx@users.noreply.github.com> --- apps/web/src/components/chat/CodeBlock.tsx | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/apps/web/src/components/chat/CodeBlock.tsx b/apps/web/src/components/chat/CodeBlock.tsx index a86bfe9584..bf938d9f67 100644 --- a/apps/web/src/components/chat/CodeBlock.tsx +++ b/apps/web/src/components/chat/CodeBlock.tsx @@ -1,4 +1,4 @@ -import { useEffect, useRef, useState } from 'preact/hooks'; +import { useEffect, useRef, useState, useMemo } from 'preact/hooks'; import { Check, Copy } from 'lucide-preact'; import { copyText } from '../../lib/clipboard'; import { highlightCode, normalizeLang, type CodeToken } from '../../lib/highlight'; @@ -26,7 +26,18 @@ export function CodeBlock({ code, lang }: { code: string; lang?: string }) { window.clearTimeout(t); }; }, [code, lang]); - const shown = tokens && tokens.map((l) => l.map((t) => t.content).join('')).join('\n') === code ? tokens : null; + const shown = useMemo(() => { + if (!tokens) return null; + let result = ''; + for (let i = 0; i < tokens.length; i++) { + if (i > 0) result += '\n'; + const lineTokens = tokens[i]; + for (let j = 0; j < lineTokens.length; j++) { + result += lineTokens[j].content; + } + } + return result === code ? tokens : null; + }, [tokens, code]); async function onCopy() { const ok = await copyText(code);