From cbb7682bf02307d2f18a87b93cb787807de54919 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Fri, 23 Jun 2023 20:33:22 -0400 Subject: [PATCH] lib: reduce url getters on `makeRequireFunction` PR-URL: https://github.com/nodejs/node/pull/48492 Refs: https://github.com/nodejs/performance/issues/92 Reviewed-By: Jacob Smith Reviewed-By: Antoine du Hamel Reviewed-By: Geoffrey Booth Reviewed-By: Luigi Pinca Reviewed-By: James M Snell --- lib/internal/modules/helpers.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/internal/modules/helpers.js b/lib/internal/modules/helpers.js index eb227e205c260f..3cdffe9e49d74c 100644 --- a/lib/internal/modules/helpers.js +++ b/lib/internal/modules/helpers.js @@ -86,8 +86,8 @@ function makeRequireFunction(mod, redirects) { if (destination === true) { missing = false; } else if (destination) { - const href = destination.href; - if (destination.protocol === 'node:') { + const { href, protocol } = destination; + if (protocol === 'node:') { const specifier = destination.pathname; if (BuiltinModule.canBeRequiredByUsers(specifier)) { @@ -95,11 +95,9 @@ function makeRequireFunction(mod, redirects) { return mod.exports; } throw new ERR_UNKNOWN_BUILTIN_MODULE(specifier); - } else if (destination.protocol === 'file:') { - let filepath; - if (urlToFileCache.has(href)) { - filepath = urlToFileCache.get(href); - } else { + } else if (protocol === 'file:') { + let filepath = urlToFileCache.get(href); + if (!filepath) { filepath = fileURLToPath(destination); urlToFileCache.set(href, filepath); }