From 0ded5e55e6860dd12fda3473ea41c6ad06fa52ad Mon Sep 17 00:00:00 2001 From: "Craig Macomber (Microsoft)" <42876482+CraigMacomber@users.noreply.github.com> Date: Mon, 8 Dec 2025 21:58:03 -0800 Subject: [PATCH 1/2] esm: avoid throw when module specifier is not url This particular exception is responsible for a lot of overhead when debugging with large esm codebases with VS Code and break on caught exceptions is enabled. VS Code silently suppresses this exception, but the mechanism it uses to do so is a bit slow so avoiding this common exception can speed up loading of esm code. In my scenario this saved over have of the startup run time (over 20 seconds). This should also make debugging without suppression of this exception more pleasant in other tools such as the Chrome dev tools when attached to NodeJs processes. --- lib/internal/modules/esm/resolve.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index a321744ab8263c..d425144728a61f 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -42,6 +42,7 @@ const { ERR_INVALID_MODULE_SPECIFIER, ERR_INVALID_PACKAGE_CONFIG, ERR_INVALID_PACKAGE_TARGET, + ERR_INVALID_URL, ERR_MODULE_NOT_FOUND, ERR_PACKAGE_IMPORT_NOT_DEFINED, ERR_PACKAGE_PATH_NOT_EXPORTED, @@ -846,12 +847,13 @@ function moduleResolve(specifier, base, conditions, preserveSymlinks) { } else if (protocol === 'file:' && specifier[0] === '#') { resolved = packageImportsResolve(specifier, base, conditions); } else { - try { - resolved = new URL(specifier); - } catch (cause) { + const url = URL.parse(specifier); + if (url !== null) { + resolved = url; + } else { if (isData && !BuiltinModule.canBeRequiredWithoutScheme(specifier)) { const error = new ERR_UNSUPPORTED_RESOLVE_REQUEST(specifier, base); - setOwnProperty(error, 'cause', cause); + setOwnProperty(error, 'cause', new ERR_INVALID_URL(specifier)); throw error; } resolved = packageResolve(specifier, base, conditions); From 95c085539a5d35dc048c3fce480f984076ffae1a Mon Sep 17 00:00:00 2001 From: "Craig Macomber (Microsoft)" <42876482+CraigMacomber@users.noreply.github.com> Date: Wed, 10 Dec 2025 09:59:43 -0800 Subject: [PATCH 2/2] Update lib/internal/modules/esm/resolve.js Use URLParse instead of URL.parse Co-authored-by: Antoine du Hamel --- lib/internal/modules/esm/resolve.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index d425144728a61f..cc1230648881d8 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -847,7 +847,7 @@ function moduleResolve(specifier, base, conditions, preserveSymlinks) { } else if (protocol === 'file:' && specifier[0] === '#') { resolved = packageImportsResolve(specifier, base, conditions); } else { - const url = URL.parse(specifier); + const url = URLParse(specifier); if (url !== null) { resolved = url; } else {