From ed76456059629f5408ea40248bed6e6e22d15ffe Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Fri, 9 Jun 2023 23:21:26 +0200 Subject: [PATCH] perf(serve): hoist repeated condition (#19449) --- ext/http/00_serve.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ext/http/00_serve.js b/ext/http/00_serve.js index 7c9b290695..a26c6604e0 100644 --- a/ext/http/00_serve.js +++ b/ext/http/00_serve.js @@ -503,16 +503,19 @@ async function asyncResponse(responseBodies, req, status, stream) { function mapToCallback(context, callback, onError) { const responseBodies = context.responseBodies; const signal = context.abortController.signal; + const hasCallback = callback.length > 0; + const hasOneCallback = callback.length === 1; + return async function (req) { // Get the response from the user-provided callback. If that fails, use onError. If that fails, return a fallback // 500 error. let innerRequest; let response; try { - if (callback.length > 0) { + if (hasCallback) { innerRequest = new InnerRequest(req, context); const request = fromInnerRequest(innerRequest, signal, "immutable"); - if (callback.length === 1) { + if (hasOneCallback) { response = await callback(request); } else { response = await callback(request, {