Skip to content

Optimize cache keys, vector search, localStorage, and React re-renders - #4

Draft
hrithiknl17 with Copilot wants to merge 3 commits into
mainfrom
copilot/improve-slow-code-performance
Draft

hrithiknl17 with Copilot wants to merge 3 commits into
mainfrom
copilot/improve-slow-code-performance

Conversation

Copilot AI commented Dec 23, 2025

Copy link
Copy Markdown

Identified and fixed 5 performance bottlenecks: inefficient cache key generation, O(n) vector similarity search, blocking localStorage operations, unnecessary React re-renders, and redundant array operations.

Cache Key Generation

  • Replaced substring-based keys with hash + length suffix to reduce collisions
  • Normalized text cache keys with trim().toLowerCase() for better hit rates
// Before: collision-prone, inconsistent
const cacheKey = prompt.substring(0, 50);

// After: stable hash with collision prevention
const hashPrompt = (prompt: string): string => {
  let hash = 0;
  for (let i = 0; i < prompt.length; i++) {
    hash = ((hash << 5) - hash) + prompt.charCodeAt(i);
    hash = hash & hash;
  }
  return `${hash.toString(36)}_${prompt.length}`;
};

Vector Similarity Search

  • Pre-compute query magnitude once before loop
  • Single-pass dot product + magnitude computation
  • Reduces computation from 3 array passes to 1
// Before: 3 passes through arrays
const dotProduct = vecA.reduce((sum, a, i) => sum + a * vecB[i], 0);
const magnitudeA = Math.sqrt(vecA.reduce((sum, a) => sum + a * a, 0));
const magnitudeB = Math.sqrt(vecB.reduce((sum, b) => sum + b * b, 0));

// After: 1 pass, pre-computed query magnitude
const queryMagnitude = Math.sqrt(queryEmbedding.reduce((sum, a) => sum + a * a, 0));
for (let i = 0; i < vecA.length; i++) {
  dotProduct += vecA[i] * vecB[i];
  sumBSquared += vecB[i] * vecB[i];
}

localStorage Operations

  • Debounced saves (500ms VectorStore, 300ms Database) to prevent main thread blocking
  • Moved data retrieval into setTimeout to prevent race conditions

React Re-renders

  • Memoized static components (Header, IdeaCard, BlueprintCard)
  • Created dedicated CaptionCard component with React.memo
  • Applied useCallback/useMemo for event handlers and computed values

Array Operations

  • Pre-computed hashtag formatting to eliminate repeated replace() calls
  • Simplified hashtag processing from map(t => '#' + t.replace('#', '')) to map(t => t.startsWith('#') ? t : '#' + t)
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 December 23, 2025 22:11
Co-authored-by: hrithiknl17 <215130962+hrithiknl17@users.noreply.github.com>
…formatting

Co-authored-by: hrithiknl17 <215130962+hrithiknl17@users.noreply.github.com>
Copilot AI changed the title [WIP] Identify and suggest improvements to inefficient code Optimize cache keys, vector search, localStorage, and React re-renders Dec 23, 2025
Copilot AI requested a review from hrithiknl17 December 23, 2025 22:16
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