// LRU using a Map to simulate an ordered dictionary (MRU at end)
function simulateLRU(refs, frames) {
const cache = new Map(); // key: page, value: true; iteration order = recency
let faults = 0;
for (const p of refs) {
if (!cache.has(p)) { // miss
faults++;
if (cache.size === frames) {
// evict LRU (first key)
const lruKey = cache.keys().next().value;
cache.delete(lruKey);
}
} else {
// refresh recency by deleting and re-inserting
cache.delete(p);
}
cache.set(p, true);
}
return faults;
}
- Stack property: fault count with k frames ≥ count with k+1 frames.
- Implementation: exact LRU is costly; approximations (reference bits, aging) are common.