From 3f27ccad800d7bd910507577953e25373ae26a8f Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Sun, 5 Jan 2025 13:31:02 +0000 Subject: [PATCH] perf(lib/utils): cache ipv4 regex (#115) --- lib/utils.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/utils.js b/lib/utils.js index 173b9fd..2ddc3fc 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -2,9 +2,11 @@ const { HEX } = require('./scopedChars') +const IPV4_REG = /^(?:(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)$/u + function normalizeIPv4 (host) { if (findToken(host, '.') < 3) { return { host, isIPV4: false } } - const matches = host.match(/^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/u) || [] + const matches = host.match(IPV4_REG) || [] const [address] = matches if (address) { return { host: stripLeadingZeros(address, '.'), isIPV4: true }