From 81304dabd3b858efd2cc1ea94b5eaa224f0afa26 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Tue, 2 Sep 2025 21:05:21 +0200 Subject: [PATCH] http: lazy allocate cookies array --- lib/_http_outgoing.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index fdd0f2f77eaac3..24aae1caca69d3 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -673,20 +673,22 @@ OutgoingMessage.prototype.setHeaders = function setHeaders(headers) { // We also cannot safely split by comma. // To avoid setHeader overwriting the previous value we push // set-cookie values in array and set them all at once. - const cookies = []; + let cookies = null; for (const { 0: key, 1: value } of headers) { if (key === 'set-cookie') { if (ArrayIsArray(value)) { + cookies ??= []; cookies.push(...value); } else { + cookies ??= []; cookies.push(value); } continue; } this.setHeader(key, value); } - if (cookies.length) { + if (cookies != null) { this.setHeader('set-cookie', cookies); }