Hard
You're building a caching system that shouldn't prevent objects from being garbage collected. What's wrong with the current implementation and which solution properly handles memory management?
1class Cache {2 constructor() {3 this.items = new Map();4 this.metadata = new Map();5 }67 // Current implementation with memory leaks8 set(key, value, metadata = {}) {9 this.items.set(key, value);10 this.metadata.set(key, {11 ...metadata,12 accessCount: 0,13 lastAccessed: Date.now()14 });15 }1617 get(key) {18 const item = this.items.get(key);19 if (item) {20 const meta = this.metadata.get(key);21 meta.accessCount++;22 meta.lastAccessed = Date.now();23 }24 return item;25 }26}