From adb43a9b4db5b158c7e5ef20ad3b43a836260a46 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 24 Apr 2026 13:40:19 +0000 Subject: [PATCH] perf: precompute static prefix in createCacheHandler Avoid dynamically allocating arrays, running `.filter(Boolean)`, and joining them on every `getFullKey` invocation. Instead, precompute the static parts (prefix and version) at initialization to use simple string concatenation. This reduces memory allocation pressure and minor CPU overhead on the hot path for cache fetches. Co-authored-by: suranig <24814104+suranig@users.noreply.github.com> --- .jules/bolt.md | 3 +++ src/cache/createCacheHandler.ts | 8 ++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 .jules/bolt.md 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(':'); }; /**