From 91ab79bb39b789d98ea46c8d762f53d5124ce19d Mon Sep 17 00:00:00 2001 From: lemonknowsall Date: Tue, 18 Nov 2025 23:01:12 -0800 Subject: [PATCH] improved performance for excessive console logs --- src/components/Shakespeare/PreviewPane.tsx | 167 ++++++++++++++++----- 1 file changed, 129 insertions(+), 38 deletions(-) diff --git a/src/components/Shakespeare/PreviewPane.tsx b/src/components/Shakespeare/PreviewPane.tsx index 2334afa6..4919efd7 100644 --- a/src/components/Shakespeare/PreviewPane.tsx +++ b/src/components/Shakespeare/PreviewPane.tsx @@ -1,5 +1,5 @@ import { encodeBase64 } from '@std/encoding/base64'; -import { useState, useEffect, useCallback, useRef } from 'react'; +import { useState, useEffect, useCallback, useRef, useMemo, memo } from 'react'; import { useTranslation } from 'react-i18next'; import { useProjectsManager } from '@/hooks/useProjectsManager'; import { useFS } from '@/hooks/useFS'; @@ -521,6 +521,7 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o // Constants const CONSOLE_SORT_ORDER_KEY = 'consoleSortOrder'; const COPY_FEEDBACK_DURATION = 2000; + const MAX_CONSOLE_MESSAGES = 500; // Limit displayed messages for performance // Helper function to get message level color const getMessageLevelColor = (level: ConsoleMessage['level']): string => { @@ -549,8 +550,8 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o ); }; - // Individual console message component - const ConsoleMessage = ({ msg, index, copiedMessageIndex, onCopy }: { + // Individual console message component - memoized for performance + const ConsoleMessage = memo(({ msg, index, copiedMessageIndex, onCopy }: { msg: ConsoleMessage; index: number; copiedMessageIndex: number | null; @@ -588,11 +589,14 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o ); - }; + }); + ConsoleMessage.displayName = 'ConsoleMessage'; const ConsoleView = () => { const [copiedMessageIndex, setCopiedMessageIndex] = useState(null); - const [messages, setMessages] = useState([]); + const [isInitialLoad, setIsInitialLoad] = useState(true); + const [rawMessages, setRawMessages] = useState([]); + const [totalMessageCount, setTotalMessageCount] = useState(0); // Load sort order from localStorage, default to 'newest-first' const [sortOrder, setSortOrder] = useState<'newest-first' | 'oldest-first'>(() => { @@ -605,23 +609,95 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o localStorage.setItem(CONSOLE_SORT_ORDER_KEY, sortOrder); }, [sortOrder]); - // Update messages from the global store + // Track rapid message bursts for intelligent batching + const lastMessageTimeRef = useRef(0); + const pendingUpdateRef = useRef(); + const messageCountSinceUpdateRef = useRef(0); + + // Update messages from the global store - optimized for immediate updates const updateMessages = useCallback(() => { - const rawMessages = getConsoleMessages(); - // Messages come in chronological order (oldest first) - // Reverse only if showing newest first - const sortedMessages = sortOrder === 'newest-first' ? [...rawMessages].reverse() : rawMessages; - setMessages(sortedMessages); - }, [sortOrder]); + const allMessages = getConsoleMessages(); + setTotalMessageCount(allMessages.length); + + // Limit messages for performance (keep most recent) + const limitedMessages = allMessages.length > MAX_CONSOLE_MESSAGES + ? allMessages.slice(-MAX_CONSOLE_MESSAGES) + : allMessages; + + setRawMessages(limitedMessages); + setIsInitialLoad(false); + messageCountSinceUpdateRef.current = 0; + }, []); + + // Smart update handler: immediate for single messages, batched for rapid bursts + const handleMessageUpdate = useCallback(() => { + const now = Date.now(); + const timeSinceLastMessage = now - lastMessageTimeRef.current; + lastMessageTimeRef.current = now; + messageCountSinceUpdateRef.current += 1; + + // Detect rapid bursts: if we get more than 3 messages within 50ms, batch them + const isRapidBurst = messageCountSinceUpdateRef.current > 3 && timeSinceLastMessage < 50; + + // Clear any pending update + if (pendingUpdateRef.current) { + cancelAnimationFrame(pendingUpdateRef.current); + pendingUpdateRef.current = undefined; + } + + if (isRapidBurst) { + // Batch rapid updates using requestAnimationFrame for smooth rendering + // This prevents UI blocking during loops that log many messages + pendingUpdateRef.current = requestAnimationFrame(() => { + updateMessages(); + }); + } else { + // Immediate update for normal console.log calls (like browser console) + // Single messages appear instantly, just like native browser console + updateMessages(); + } + }, [updateMessages]); // Initial load and listener setup useEffect(() => { - updateMessages(); - addConsoleMessageListener(updateMessages); + // Initial load - check if we have many messages + const allMessages = getConsoleMessages(); + if (allMessages.length > 500) { + setIsInitialLoad(true); + // Use requestIdleCallback for non-blocking initial render, fallback to setTimeout + const scheduleUpdate = () => { + if ('requestIdleCallback' in window) { + requestIdleCallback(() => { + updateMessages(); + }, { timeout: 100 }); + } else { + setTimeout(() => { + updateMessages(); + }, 0); + } + }; + scheduleUpdate(); + } else { + updateMessages(); + } + + addConsoleMessageListener(handleMessageUpdate); return () => { - removeConsoleMessageListener(updateMessages); + removeConsoleMessageListener(handleMessageUpdate); + if (pendingUpdateRef.current) { + cancelAnimationFrame(pendingUpdateRef.current); + } }; - }, [updateMessages]); + }, [handleMessageUpdate, updateMessages]); + + // Memoize sorted messages to avoid recalculation on every render + const messages = useMemo(() => { + if (sortOrder === 'newest-first') { + // For newest-first, reverse the array (messages are stored oldest-first) + return [...rawMessages].reverse(); + } + return rawMessages; + }, [rawMessages, sortOrder]); const copyMessageToClipboard = async (msg: ConsoleMessage, index: number) => { try { @@ -639,6 +715,7 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o }; const messageCount = messages.length; + const hasMoreMessages = rawMessages.length >= MAX_CONSOLE_MESSAGES; return (
@@ -656,6 +733,11 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o
{messageCount > 0 && ( <> + {hasMoreMessages && ( + + Showing last {MAX_CONSOLE_MESSAGES.toLocaleString()} of {totalMessageCount.toLocaleString()} + + )}