From 0b167aaa35f875effca8f18564a07d07b56ec48f Mon Sep 17 00:00:00 2001 From: Chris Ham <431647+greenham@users.noreply.github.com> Date: Sat, 16 Aug 2025 15:21:50 -0700 Subject: [PATCH] Add high-performance caching for SFX autocomplete MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Pre-sort SFX names at startup for efficient autocomplete - Add search result caching with Map for instant repeated queries - Respect Discord's 25-choice autocomplete limit - Cache invalidation when SFX directory changes - Optimize for 275+ sound effects with minimal latency Performance improvements: - Autocomplete responses now cached and instant - No file system access during user interactions - Memory-efficient search result caching 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/services/sfxManager.js | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/src/services/sfxManager.js b/src/services/sfxManager.js index f2cc462..0b233ad 100644 --- a/src/services/sfxManager.js +++ b/src/services/sfxManager.js @@ -5,6 +5,8 @@ class SFXManager { constructor() { this.sfxPath = path.join(__dirname, '..', '..', 'sfx'); this.sfxList = []; + this.cachedNames = []; + this.searchCache = new Map(); // Cache for autocomplete searches // Load SFX list initially this.loadSFXList(); @@ -35,6 +37,14 @@ class SFXManager { }; }); + // Cache sorted names for autocomplete + this.cachedNames = this.sfxList + .map(sfx => sfx.name) + .sort((a, b) => a.localeCompare(b)); + + // Clear search cache when SFX list changes + this.searchCache.clear(); + console.log(`Loaded ${this.sfxList.length} sound effects`); } catch (error) { console.error('Error loading SFX list:', error); @@ -62,11 +72,11 @@ class SFXManager { } /** - * Get SFX names for autocomplete + * Get SFX names for autocomplete (cached and sorted) * @returns {Array} List of SFX names */ getSFXNames() { - return this.sfxList.map(sfx => sfx.name); + return this.cachedNames; } /** @@ -98,15 +108,27 @@ class SFXManager { } /** - * Search SFX names (for autocomplete) + * Search SFX names (for autocomplete) with caching * @param {string} query * @returns {Array} Matching SFX names */ searchSFX(query) { const lowerQuery = query.toLowerCase(); - return this.sfxList - .filter(sfx => sfx.name.toLowerCase().includes(lowerQuery)) - .map(sfx => sfx.name); + + // Check cache first + if (this.searchCache.has(lowerQuery)) { + return this.searchCache.get(lowerQuery); + } + + // Perform search on cached names (already sorted) + const results = this.cachedNames + .filter(name => name.toLowerCase().includes(lowerQuery)) + .slice(0, 25); // Discord autocomplete limit + + // Cache the result for future use + this.searchCache.set(lowerQuery, results); + + return results; } }