Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,11 @@ Action: Apply `express.json()` strictly as route-specific middleware to the exac

Learning: In Express API gateways, declaring high-frequency, simple endpoints (like `/health`) below global middleware such as `helmet` and `cors` introduces significant and unnecessary CPU parsing overhead for every ping, even if the ping does not require CORS or security headers.
Action: Moved the `/health` endpoint definition above `helmet()` and `cors()` in `src/index.js`, while manually explicitly setting the `Content-Type` header. This drastically reduces CPU overhead and latency for load balancer pings while maintaining correct response headers.

## 2026-04-27 — Optimize Cache Lookups

Learning:
In highly trafficked functions like `heavyComputation` that rely on an LRU map cache, repeated calls with identical parameters incur significant overhead due to Map `delete` and `set` operations used to refresh the LRU order.

Action:
Added an L1 cache using module-scoped variables (`lastIterations` and `lastResult`) to `heavyComputation` in `src/index.js`. This avoids redundant Map lookups and mutations for consecutive identical calls, transforming a hot path from an (1)$ Map operation to a much faster strict equality check.
15 changes: 15 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,28 @@ app.use((err, req, res, next) => {
});

const computationCache = new Map();
let lastIterations = null;
let lastResult = null;

/**
* Performs a heavy mathematical computation.
* Optimized with memoization for repeated calls with identical parameters.
*/
function heavyComputation(iterations) {
// L1 Cache: Fast path for repeated consecutive calls
if (iterations === lastIterations) {
return lastResult;
}

const cachedVal = computationCache.get(iterations);
if (cachedVal !== undefined) {
// Refresh LRU by deleting and re-inserting
computationCache.delete(iterations);
computationCache.set(iterations, cachedVal);

// Update L1 cache
lastIterations = iterations;
lastResult = cachedVal;
return cachedVal;
}

Expand All @@ -180,6 +191,10 @@ function heavyComputation(iterations) {
}

computationCache.set(iterations, sum);

// Update L1 cache
lastIterations = iterations;
lastResult = sum;
return sum;
}

Expand Down
Loading