From 6c36de39e22f083e3005e49a66f762abb3b3519c Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Thu, 17 Oct 2024 23:06:50 +0300 Subject: [PATCH 1/2] esm: optimize string checks and slicing with direct index access --- lib/internal/modules/esm/fetch_module.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/internal/modules/esm/fetch_module.js b/lib/internal/modules/esm/fetch_module.js index 07904e685becf8..cbe659b2d3a782 100644 --- a/lib/internal/modules/esm/fetch_module.js +++ b/lib/internal/modules/esm/fetch_module.js @@ -3,9 +3,6 @@ const { ObjectPrototypeHasOwnProperty, PromisePrototypeThen, SafeMap, - StringPrototypeEndsWith, - StringPrototypeSlice, - StringPrototypeStartsWith, } = primordials; const { Buffer: { concat: BufferConcat }, @@ -248,10 +245,10 @@ allowList.addRange('127.0.0.1', '127.255.255.255'); async function isLocalAddress(hostname) { try { if ( - StringPrototypeStartsWith(hostname, '[') && - StringPrototypeEndsWith(hostname, ']') + hostname[0] === '[' && + hostname[hostname.length - 1] === ']' ) { - hostname = StringPrototypeSlice(hostname, 1, -1); + hostname = hostname.slice(1, -1); } const addr = await dnsLookup(hostname, { order: 'verbatim' }); const ipv = addr.family === 4 ? 'ipv4' : 'ipv6'; From 5b77adbb5d409cda82c1ec5f96dc40290e522e96 Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Thu, 17 Oct 2024 23:11:53 +0300 Subject: [PATCH 2/2] revert slice --- lib/internal/modules/esm/fetch_module.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/internal/modules/esm/fetch_module.js b/lib/internal/modules/esm/fetch_module.js index cbe659b2d3a782..8f18e9bf04e0b4 100644 --- a/lib/internal/modules/esm/fetch_module.js +++ b/lib/internal/modules/esm/fetch_module.js @@ -3,6 +3,7 @@ const { ObjectPrototypeHasOwnProperty, PromisePrototypeThen, SafeMap, + StringPrototypeSlice, } = primordials; const { Buffer: { concat: BufferConcat }, @@ -248,7 +249,7 @@ async function isLocalAddress(hostname) { hostname[0] === '[' && hostname[hostname.length - 1] === ']' ) { - hostname = hostname.slice(1, -1); + hostname = StringPrototypeSlice(hostname, 1, -1); } const addr = await dnsLookup(hostname, { order: 'verbatim' }); const ipv = addr.family === 4 ? 'ipv4' : 'ipv6';