SOURCE

// https://zhuanlan.zhihu.com/p/161269766
// 内存淘汰算法
class LRU {
    max = 5;
    zoom = [];
    constructor(max, zoom) {
        this.max = max;
        this.zoom = zoom;
    }

    use = (item) => {
        if (this.zoom.includes(item)) {
            const newZoom = [];
            let lastItem;
            this.zoom.forEach(e => {
                if (e !== item) {
                    newZoom.push(e)
                } else {
                    lastItem = item;
                }
            })
            newZoom.push(lastItem);
            this.zoom = newZoom;
        } else {
            if (this.zoom.length < this.max) {
                this.zoom.push(item);
            } else {
                this.zoom.shift();
                this.zoom.push(item);
            }
        }
    }
}

const lru = new LRU(4, ['a', 'b', 'c', 'd']);
console.log(lru.zoom)
lru.use('a');
lru.use('e');
lru.use('f');
lru.use('d');
lru.use('a');
lru.use('r');
console.log(lru.zoom)
console 命令行工具 X clear

                    
>
console