Performance: Why caching reduces response latency
HIGH IMPACT
Caching reduces the time it takes for the server to respond by avoiding repeated heavy computations or database calls.
private cache = new Map<string, User>(); async getUserData(userId: string) { if (this.cache.has(userId)) { return this.cache.get(userId); } const user = await this.database.findUserById(userId); this.cache.set(userId, user); return user; }
async getUserData(userId: string) { return await this.database.findUserById(userId); }
| Pattern | Server Processing | Database Queries | Response Time | Verdict |
|---|---|---|---|---|
| No caching | High CPU and I/O | One query per request | 50-200ms | [X] Bad |
| With caching | Minimal CPU | Zero queries on cache hit | Under 5ms | [OK] Good |