diff --git a/lib/internal/fixed_queue.js b/lib/internal/fixed_queue.js index a6e00c716c5371..91aa98e8f6fe14 100644 --- a/lib/internal/fixed_queue.js +++ b/lib/internal/fixed_queue.js @@ -89,8 +89,10 @@ class FixedCircularBuffer { } module.exports = class FixedQueue { + static #pool = []; + constructor() { - this.head = this.tail = new FixedCircularBuffer(); + this.head = this.tail = FixedQueue.#pool.pop() ?? new FixedCircularBuffer(); } isEmpty() { @@ -101,7 +103,7 @@ module.exports = class FixedQueue { if (this.head.isFull()) { // Head is full: Creates a new queue, sets the old queue's `.next` to it, // and sets it as the new main queue. - this.head = this.head.next = new FixedCircularBuffer(); + this.head = this.head.next = FixedQueue.#pool.pop() ?? new FixedCircularBuffer(); } this.head.push(data); } @@ -113,6 +115,12 @@ module.exports = class FixedQueue { // If there is another queue, it forms the new tail. this.tail = tail.next; tail.next = null; + tail.bottom = 0; + tail.top = 0; + + if (FixedQueue.#pool.length < 64) { + FixedQueue.#pool.push(tail); // Recycle old tail + } } return next; }