From edab50c40bc88a8443a86afc8e05c4170a7679fc 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/cjs/helpers.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/internal/modules/cjs/helpers.js b/lib/internal/modules/cjs/helpers.js index dd15055aee1fd5..00d2b628c44172 100644 --- a/lib/internal/modules/cjs/helpers.js +++ b/lib/internal/modules/cjs/helpers.js @@ -87,19 +87,17 @@ 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; const mod = loadBuiltinModule(specifier, href); if (mod && mod.canBeRequiredByUsers) { 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); }