Add high-performance caching for SFX autocomplete

- 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 <noreply@anthropic.com>
This commit is contained in:
Chris Ham
2025-08-16 15:21:50 -07:00
parent c3adb66de8
commit 0b167aaa35

View File

@@ -5,6 +5,8 @@ class SFXManager {
constructor() { constructor() {
this.sfxPath = path.join(__dirname, '..', '..', 'sfx'); this.sfxPath = path.join(__dirname, '..', '..', 'sfx');
this.sfxList = []; this.sfxList = [];
this.cachedNames = [];
this.searchCache = new Map(); // Cache for autocomplete searches
// Load SFX list initially // Load SFX list initially
this.loadSFXList(); 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`); console.log(`Loaded ${this.sfxList.length} sound effects`);
} catch (error) { } catch (error) {
console.error('Error loading SFX list:', 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 * @returns {Array} List of SFX names
*/ */
getSFXNames() { 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 * @param {string} query
* @returns {Array} Matching SFX names * @returns {Array} Matching SFX names
*/ */
searchSFX(query) { searchSFX(query) {
const lowerQuery = query.toLowerCase(); const lowerQuery = query.toLowerCase();
return this.sfxList
.filter(sfx => sfx.name.toLowerCase().includes(lowerQuery)) // Check cache first
.map(sfx => sfx.name); 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;
} }
} }