当前位置:首页 > JavaScript

js实现lru

2026-02-01 08:12:32JavaScript

LRU缓存算法的实现

LRU(Least Recently Used)是一种缓存淘汰策略,当缓存空间不足时,会优先淘汰最近最少使用的数据。以下是使用JavaScript实现LRU缓存的几种方法:

js实现lru

使用Map实现LRU

Map对象保存键值对,并且能记住键的原始插入顺序,这使得它非常适合实现LRU缓存。

js实现lru

class LRUCache {
  constructor(capacity) {
    this.capacity = capacity;
    this.cache = new Map();
  }

  get(key) {
    if (!this.cache.has(key)) return -1;

    const value = this.cache.get(key);
    this.cache.delete(key);
    this.cache.set(key, value);
    return value;
  }

  put(key, value) {
    if (this.cache.has(key)) {
      this.cache.delete(key);
    }

    this.cache.set(key, value);

    if (this.cache.size > this.capacity) {
      const oldestKey = this.cache.keys().next().value;
      this.cache.delete(oldestKey);
    }
  }
}

使用双向链表和哈希表实现

这种方法结合了双向链表的高效删除/插入和哈希表的快速查找特性。

class Node {
  constructor(key, value) {
    this.key = key;
    this.value = value;
    this.prev = null;
    this.next = null;
  }
}

class LRUCache {
  constructor(capacity) {
    this.capacity = capacity;
    this.size = 0;
    this.cache = {};
    this.head = new Node(0, 0);
    this.tail = new Node(0, 0);
    this.head.next = this.tail;
    this.tail.prev = this.head;
  }

  _addNode(node) {
    node.prev = this.head;
    node.next = this.head.next;
    this.head.next.prev = node;
    this.head.next = node;
  }

  _removeNode(node) {
    const prev = node.prev;
    const next = node.next;
    prev.next = next;
    next.prev = prev;
  }

  _moveToHead(node) {
    this._removeNode(node);
    this._addNode(node);
  }

  _popTail() {
    const res = this.tail.prev;
    this._removeNode(res);
    return res;
  }

  get(key) {
    const node = this.cache[key];
    if (!node) return -1;
    this._moveToHead(node);
    return node.value;
  }

  put(key, value) {
    const node = this.cache[key];
    if (!node) {
      const newNode = new Node(key, value);
      this.cache[key] = newNode;
      this._addNode(newNode);
      this.size++;

      if (this.size > this.capacity) {
        const tail = this._popTail();
        delete this.cache[tail.key];
        this.size--;
      }
    } else {
      node.value = value;
      this._moveToHead(node);
    }
  }
}

使用ES6 Proxy实现

Proxy可以拦截对象的操作,可以用来实现LRU的自动管理。

function createLRUCache(capacity) {
  const cache = new Map();

  return new Proxy({}, {
    get(target, prop) {
      if (cache.has(prop)) {
        const value = cache.get(prop);
        cache.delete(prop);
        cache.set(prop, value);
        return value;
      }
      return undefined;
    },
    set(target, prop, value) {
      if (cache.has(prop)) {
        cache.delete(prop);
      }
      cache.set(prop, value);

      if (cache.size > capacity) {
        const oldestKey = cache.keys().next().value;
        cache.delete(oldestKey);
      }
      return true;
    }
  });
}

性能比较

  1. Map实现简单,但删除操作需要遍历
  2. 双向链表实现性能最优,但代码复杂度高
  3. Proxy实现最简洁,但可能在某些环境下有兼容性问题

选择哪种实现方式取决于具体应用场景和性能要求。对于大多数应用场景,Map实现已经足够高效且易于维护。

标签: jslru
分享给朋友:

相关文章

css3结合js制作

css3结合js制作

CSS3 结合 JavaScript 制作动态效果 CSS3 和 JavaScript 结合可以实现丰富的动态效果,包括动画、交互和响应式设计。以下是几种常见的实现方式: CSS3 动画与 Java…

js实现继承

js实现继承

原型链继承 通过将子类的原型指向父类的实例实现继承。子类实例可以访问父类原型上的属性和方法。 function Parent() { this.name = 'parent'; } Parent…

js实现图片上传

js实现图片上传

图片上传的基本实现 使用HTML的<input type="file">元素配合JavaScript的File API可以实现图片上传功能。HTML部分需要创建一个文件选择输入框和一个用于…

js画图实现

js画图实现

使用Canvas API绘制图形 Canvas是HTML5提供的绘图API,通过JavaScript操作Canvas元素可以绘制各种图形。以下是一个简单的示例: <canvas id="myC…

js实现授权

js实现授权

授权流程设计 授权流程通常涉及前端与后端的交互,常见方案包括OAuth2.0、JWT等。以JWT为例的典型流程: 用户提交凭证(如用户名密码)到认证服务 服务端验证通过后生成包含用户信息的JWT令牌…

利用js实现

利用js实现

使用 JavaScript 实现 在 JavaScript 中,可以通过多种方式实现功能,具体取决于需求。以下是几种常见的方法: 方法一:使用原生 JavaScript // 示例代码…