Performance: Memory management best practices
HIGH IMPACT
This concept affects server-side memory usage and response time, impacting how quickly Node.js can handle requests without slowing down or crashing.
const cache = new Map();
function handleRequest(data) {
cache.set(data.id, data);
if (cache.size > 1000) {
const firstKey = cache.keys().next().value;
cache.delete(firstKey);
}
}const cache = {};
function handleRequest(data) {
cache[data.id] = data;
// never delete or clear cache
}| Pattern | Memory Usage | Garbage Collection Frequency | Response Time Impact | Verdict |
|---|---|---|---|---|
| Unbounded cache without cleanup | High and growing | Frequent and long pauses | Slower and unstable | [X] Bad |
| Bounded cache with cleanup | Stable and limited | Infrequent and short pauses | Fast and stable | [OK] Good |