From 2e5412f63ad74f2d3c8946ec4cc8f4b5d79aed23 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Wed, 21 Jan 2026 14:10:53 +0100 Subject: [PATCH] lib: recycle queues Avoid creating unnecessary garbage in the old space. --- lib/internal/fixed_queue.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) 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; }