当前位置:首页 > JavaScript

js 实现消息队列

2026-04-07 11:34:10JavaScript

实现消息队列的基本思路

使用 JavaScript 实现消息队列的核心是利用数组或链表结构存储消息,并通过先进先出(FIFO)的原则处理消息。消息队列通常包含入队(enqueue)和出队(dequeue)操作,以及队列状态监控等功能。

js 实现消息队列

基于数组的简单消息队列

class MessageQueue {
  constructor() {
    this.queue = [];
  }

  enqueue(message) {
    this.queue.push(message);
  }

  dequeue() {
    return this.queue.shift();
  }

  isEmpty() {
    return this.queue.length === 0;
  }

  size() {
    return this.queue.length;
  }
}

基于链表的消息队列

链表实现可以避免数组 shift() 操作的高时间复杂度(O(n)),提升性能。

js 实现消息队列

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

class LinkedListMessageQueue {
  constructor() {
    this.head = null;
    this.tail = null;
    this.length = 0;
  }

  enqueue(value) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      this.tail = newNode;
    } else {
      this.tail.next = newNode;
      this.tail = newNode;
    }
    this.length++;
  }

  dequeue() {
    if (!this.head) return null;
    const value = this.head.value;
    this.head = this.head.next;
    this.length--;
    if (this.length === 0) this.tail = null;
    return value;
  }

  isEmpty() {
    return this.length === 0;
  }

  size() {
    return this.length;
  }
}

支持异步处理的扩展消息队列

结合 Promise 或 async/await 实现异步消息处理,适用于需要等待任务完成的场景。

class AsyncMessageQueue {
  constructor() {
    this.queue = [];
    this.resolveMap = new Map();
  }

  enqueue(message) {
    return new Promise((resolve) => {
      if (this.resolveMap.size === 0) {
        this.queue.push(message);
        resolve();
      } else {
        const oldestResolve = this.resolveMap.keys().next().value;
        oldestResolve(message);
        this.resolveMap.delete(oldestResolve);
      }
    });
  }

  dequeue() {
    return new Promise((resolve) => {
      if (this.queue.length > 0) {
        resolve(this.queue.shift());
      } else {
        this.resolveMap.set(resolve, true);
      }
    });
  }
}

使用场景示例

消息队列适用于任务调度、事件处理、缓冲请求等场景。例如,在 Web 应用中处理高并发请求时,可以用消息队列缓冲任务。

const queue = new MessageQueue();

// 生产者
queue.enqueue("Task 1");
queue.enqueue("Task 2");

// 消费者
while (!queue.isEmpty()) {
  const task = queue.dequeue();
  console.log("Processing:", task);
}

性能优化与注意事项

  • 避免频繁内存操作:链表实现更适合高频入队/出队场景。
  • 错误处理:增加队列溢出或空队列处理的逻辑。
  • 并发控制:多线程环境下需使用锁机制(如 Web Worker 的原子操作)。

标签: 队列消息
分享给朋友:

相关文章

vue实现消息通知功能

vue实现消息通知功能

Vue 实现消息通知功能 使用 Vue 的自定义事件系统 Vue 的自定义事件系统可以用于在组件之间传递消息。通过 $emit 触发事件,$on 监听事件,可以实现简单的消息通知功能。 // 父组件…

vue实现消息提醒功能

vue实现消息提醒功能

Vue 实现消息提醒功能的方法 使用 Vue 的 v-if 或 v-show 控制显示 通过 Vue 的指令 v-if 或 v-show 可以动态控制消息提醒的显示与隐藏。v-if 是条件渲染,v-s…

vue实现滚动推送消息

vue实现滚动推送消息

vue实现滚动推送消息的方法 使用CSS动画实现滚动 通过CSS的animation和@keyframes实现无限滚动效果,适合简单的单行消息推送。 <template> <d…

vue view实现消息推送

vue view实现消息推送

Vue 实现消息推送的方案 使用 WebSocket 实现实时通信 在 Vue 中集成 WebSocket 可以建立持久连接,实现服务器到客户端的实时消息推送。需要创建一个 WebSocket 实例并…

vue怎么实现消息提醒

vue怎么实现消息提醒

实现消息提醒的方法 在Vue中实现消息提醒通常可以通过以下几种方式完成,具体选择取决于项目需求和复杂度。 使用Vue的响应式特性 通过Vue的data属性和v-if或v-show指令,可以快速实现简…

vue消息提示怎么实现

vue消息提示怎么实现

Vue 消息提示的实现方法 使用 Element UI 的 Message 组件 Element UI 提供了 ElMessage 组件,用于显示全局消息提示。安装 Element UI 后可以直接使…