diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..dd754e1 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2026-04-24 - Precomputing Static Prefixes on Hot Paths +**Learning:** In Node.js, dynamic array allocation (`[]`), array methods like `.filter(Boolean)`, and `.join(':')` inside a hot path (like cache key generation) introduce unnecessary CPU overhead and memory allocation pressure. This is a common performance anti-pattern. +**Action:** Always precompute static segments of strings outside the function scope (e.g. at initialization). Use simple string concatenation or template literals (`${prefix}${key}`) on the hot path to construct keys efficiently. diff --git a/src/cache/createCacheHandler.ts b/src/cache/createCacheHandler.ts index 4a7fd12..b7d1c45 100644 --- a/src/cache/createCacheHandler.ts +++ b/src/cache/createCacheHandler.ts @@ -61,12 +61,16 @@ export function createCacheHandler(options: CacheHandlerOptions) const l1Cache = new Map(); const L1_CACHE_TTL = 1000; // 1 second TTL for L1 cache + // Precompute base prefix to avoid array allocations, .filter(), and .join() on every fetch (hot path) + const baseParts = [prefix, version].filter(Boolean); + const basePrefix = baseParts.length > 0 ? `${baseParts.join(':')}:` : ''; + /** * Get the fully qualified key with prefix and version */ const getFullKey = (key: string): string => { - const parts = [prefix, version, key].filter(Boolean); - return parts.join(':'); + // Check for empty key edge case to match original .filter(Boolean) behavior + return key ? `${basePrefix}${key}` : baseParts.join(':'); }; /**