Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/internal/fixed_queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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);
}
Expand All @@ -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;
}
Expand Down
Loading