当前位置:首页 > JavaScript

js实现lru

2026-04-06 00:46:09JavaScript

LRU缓存算法的实现思路

LRU(Least Recently Used)缓存算法是一种常见的缓存淘汰策略,其核心思想是淘汰最久未使用的数据。在JavaScript中可以通过Map或双向链表实现。

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);
    } else if (this.cache.size >= this.capacity) {
      const oldestKey = this.cache.keys().next().value;
      this.cache.delete(oldestKey);
    }
    this.cache.set(key, value);
  }
}

双向链表+哈希表实现

更高效的实现方式是使用双向链表配合哈希表,可以在O(1)时间内完成所有操作。

class ListNode {
  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 ListNode(0, 0);
    this.tail = new ListNode(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 ListNode(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);
    }
  }
}

两种实现方式的比较

Map实现方式代码更简洁,适合容量不大的缓存场景。双向链表实现方式性能更高,特别是在频繁操作的大容量缓存场景下,能保证O(1)时间复杂度。

在实际应用中,如果缓存容量较小且对性能要求不高,可以选择Map实现。对于需要高性能的大规模缓存系统,推荐使用双向链表+哈希表的实现方式。

标签: jslru
分享给朋友:

相关文章

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 实现页面跳转,这是最常用的方法。 window.location.href = 'https://…

js类实现

js类实现

使用类实现 JavaScript 功能 在 JavaScript 中,类(Class)是一种语法糖,基于原型继承机制。通过 class 关键字可以更直观地定义对象模板。 基本类定义 class…

js实现视口

js实现视口

js实现视口检测的方法 使用JavaScript检测元素是否进入视口(viewport)可以通过Intersection Observer API或手动计算元素位置实现。以下是两种常见方法: Int…

js实现上传文件

js实现上传文件

文件上传的基本实现 使用HTML的<input type="file">元素配合JavaScript的File API可以实现文件上传功能。 <input type="file"…

js实现菜单

js实现菜单

实现基本HTML结构 使用HTML创建菜单的基本框架,通常包含<ul>和<li>元素。示例结构如下: <ul id="menu"> <li><…

js实现复选框

js实现复选框

实现复选框的基本方法 使用HTML的<input type="checkbox">元素创建复选框,并通过JavaScript监听其状态变化。 <input type="checkb…