Skip to content

Optimize regex compilation, React memoization, and memory management#1

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/improve-slow-code-efficiency
Draft

Optimize regex compilation, React memoization, and memory management#1
Copilot wants to merge 3 commits intomainfrom
copilot/improve-slow-code-efficiency

Conversation

Copy link
Copy Markdown

Copilot AI commented Oct 28, 2025

Identified and resolved performance bottlenecks causing inefficient CPU usage, excessive memory growth, and unnecessary React re-renders.

Server (server.js)

  • Precompiled ANSI regex - Moved pattern to module scope with proper lastIndex reset to eliminate regex recreation overhead on every data event
  • Conditional logging - Gated verbose console output behind NODE_ENV checks
  • Connection limiting - Enforced MAX_CONNECTIONS (100) with graceful rejection to prevent resource exhaustion

Client (App.tsx)

  • Memoization - Wrapped computed values (getButtonText, getButtonClass) in useMemo and event handlers (handleDisconnect, handleTerminalInput) in useCallback to prevent unnecessary re-renders
  • Bounded message history - Capped array at 1000 messages, trimming to 900 to prevent unbounded growth
  • Reduced string operations - Simplified message status checks from multiple conditionals to single else-if chain

Renderer (renderer.ts)

  • MutationObserver - Set attributeOldValue: false to reduce memory overhead

Before:

function cleanAnsiSequences(text) {
    return text.replace(/\x1b\[[0-9;?]*[a-zA-Z]/g, '');  // Regex recreated every call
}

After:

const ansiRegex = /\x1b\[[0-9;?]*[a-zA-Z]/g;  // Compiled once
function cleanAnsiSequences(text) {
    ansiRegex.lastIndex = 0;
    return text.replace(ansiRegex, '');
}
Original prompt

Identify and suggest improvements to slow or inefficient code


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits October 28, 2025 21:13
…act memoization

Co-authored-by: NSYCoding <130294682+NSYCoding@users.noreply.github.com>
Co-authored-by: NSYCoding <130294682+NSYCoding@users.noreply.github.com>
Copilot AI changed the title [WIP] Identify and suggest improvements for slow code Optimize regex compilation, React memoization, and memory management Oct 28, 2025
Copilot AI requested a review from NSYCoding October 28, 2025 21:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants