From 03b5892560bbe2d3e980ed60126d257cbfd546c5 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Sat, 9 Sep 2023 01:42:49 +0200 Subject: [PATCH 1/5] net: improve performance of isIPv4 and isIPv6 --- lib/internal/net.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/internal/net.js b/lib/internal/net.js index bcbaaa94989e28..4bdeb58d678cf0 100644 --- a/lib/internal/net.js +++ b/lib/internal/net.js @@ -11,22 +11,22 @@ const { writeBuffer } = internalBinding('fs'); const errors = require('internal/errors'); // IPv4 Segment -const v4Seg = '(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'; -const v4Str = `(${v4Seg}[.]){3}${v4Seg}`; +const v4Seg = '(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])'; +const v4Str = `(?:${v4Seg}\.){3}${v4Seg}`; const IPv4Reg = new RegExp(`^${v4Str}$`); // IPv6 Segment const v6Seg = '(?:[0-9a-fA-F]{1,4})'; -const IPv6Reg = new RegExp('^(' + +const IPv6Reg = new RegExp('^(?:' + `(?:${v6Seg}:){7}(?:${v6Seg}|:)|` + `(?:${v6Seg}:){6}(?:${v4Str}|:${v6Seg}|:)|` + - `(?:${v6Seg}:){5}(?::${v4Str}|(:${v6Seg}){1,2}|:)|` + - `(?:${v6Seg}:){4}(?:(:${v6Seg}){0,1}:${v4Str}|(:${v6Seg}){1,3}|:)|` + - `(?:${v6Seg}:){3}(?:(:${v6Seg}){0,2}:${v4Str}|(:${v6Seg}){1,4}|:)|` + - `(?:${v6Seg}:){2}(?:(:${v6Seg}){0,3}:${v4Str}|(:${v6Seg}){1,5}|:)|` + - `(?:${v6Seg}:){1}(?:(:${v6Seg}){0,4}:${v4Str}|(:${v6Seg}){1,6}|:)|` + - `(?::((?::${v6Seg}){0,5}:${v4Str}|(?::${v6Seg}){1,7}|:))` + -')(%[0-9a-zA-Z-.:]{1,})?$'); + `(?:${v6Seg}:){5}(?::${v4Str}|(?::${v6Seg}){1,2}|:)|` + + `(?:${v6Seg}:){4}(?:(?::${v6Seg}){0,1}:${v4Str}|(?::${v6Seg}){1,3}|:)|` + + `(?:${v6Seg}:){3}(?:(?::${v6Seg}){0,2}:${v4Str}|(?::${v6Seg}){1,4}|:)|` + + `(?:${v6Seg}:){2}(?:(?::${v6Seg}){0,3}:${v4Str}|(?::${v6Seg}){1,5}|:)|` + + `(?:${v6Seg}:){1}(?:(?::${v6Seg}){0,4}:${v4Str}|(?::${v6Seg}){1,6}|:)|` + + `(?::(?:(?::${v6Seg}){0,5}:${v4Str}|(?::${v6Seg}){1,7}|:))` + +')(?:%[0-9a-zA-Z-.:]{1,})?$'); function isIPv4(s) { // TODO(aduh95): Replace RegExpPrototypeTest with RegExpPrototypeExec when it From db898b0a1befda022d3f0bef5161a91eb88e4cd3 Mon Sep 17 00:00:00 2001 From: Uzlopak Date: Sat, 9 Sep 2023 02:05:58 +0200 Subject: [PATCH 2/5] Update lib/internal/net.js --- lib/internal/net.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/net.js b/lib/internal/net.js index 4bdeb58d678cf0..8b04d5f226eb17 100644 --- a/lib/internal/net.js +++ b/lib/internal/net.js @@ -12,7 +12,7 @@ const errors = require('internal/errors'); // IPv4 Segment const v4Seg = '(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])'; -const v4Str = `(?:${v4Seg}\.){3}${v4Seg}`; +const v4Str = `(?:${v4Seg}\\.){3}${v4Seg}`; const IPv4Reg = new RegExp(`^${v4Str}$`); // IPv6 Segment From 162981ec3d4cebd97605990b470d9ae6af546695 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Sat, 9 Sep 2023 15:30:49 +0200 Subject: [PATCH 3/5] add benchmarks --- benchmark/net/net-is-ip-v4.js | 22 ++++++++++++++++++++++ benchmark/net/net-is-ip-v6.js | 22 ++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 benchmark/net/net-is-ip-v4.js create mode 100644 benchmark/net/net-is-ip-v6.js diff --git a/benchmark/net/net-is-ip-v4.js b/benchmark/net/net-is-ip-v4.js new file mode 100644 index 00000000000000..ab07bbeb1aed81 --- /dev/null +++ b/benchmark/net/net-is-ip-v4.js @@ -0,0 +1,22 @@ +'use strict'; + +const common = require('../common.js'); +const { isIPv4 } = require('net'); + +const minIPv4 = '0.0.0.0'; +const maxIPv4 = '255.255.255.255'; +const invalid = '0.0.0.0.0'; + +const bench = common.createBenchmark(main, { + n: [1e7], +}); + +function main({ n }) { + bench.start(); + for (let i = 0; i < n; ++i) { + isIPv4(minIPv4); + isIPv4(maxIPv4); + isIPv4(invalid); + } + bench.end(n); +} diff --git a/benchmark/net/net-is-ip-v6.js b/benchmark/net/net-is-ip-v6.js new file mode 100644 index 00000000000000..484d3b41c0002a --- /dev/null +++ b/benchmark/net/net-is-ip-v6.js @@ -0,0 +1,22 @@ +'use strict'; + +const common = require('../common.js'); +const { isIPv6 } = require('net'); + +const minIPv6 = '::1'; +const maxIPv6 = 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'; +const invalid = '0.0.0.0'; + +const bench = common.createBenchmark(main, { + n: [1e7], +}); + +function main({ n }) { + bench.start(); + for (let i = 0; i < n; ++i) { + isIPv6(minIPv6); + isIPv6(maxIPv6); + isIPv6(invalid); + } + bench.end(n); +} From 87dfd8afc7c148d2bae09f2fbe6a792451a7147a Mon Sep 17 00:00:00 2001 From: uzlopak Date: Sun, 10 Sep 2023 21:00:18 +0200 Subject: [PATCH 4/5] improve benchmarks as requested --- benchmark/net/net-is-ip-v4.js | 15 +++++++++------ benchmark/net/net-is-ip-v6.js | 13 +++++++------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/benchmark/net/net-is-ip-v4.js b/benchmark/net/net-is-ip-v4.js index ab07bbeb1aed81..76fcdfe09a9920 100644 --- a/benchmark/net/net-is-ip-v4.js +++ b/benchmark/net/net-is-ip-v4.js @@ -3,9 +3,13 @@ const common = require('../common.js'); const { isIPv4 } = require('net'); -const minIPv4 = '0.0.0.0'; -const maxIPv4 = '255.255.255.255'; -const invalid = '0.0.0.0.0'; +const ips = [ + '0.0.0.0', + '255.255.255.255', + '0.0.0.0.0', + '192.168.0.1', + '10.168.209.250', +]; const bench = common.createBenchmark(main, { n: [1e7], @@ -14,9 +18,8 @@ const bench = common.createBenchmark(main, { function main({ n }) { bench.start(); for (let i = 0; i < n; ++i) { - isIPv4(minIPv4); - isIPv4(maxIPv4); - isIPv4(invalid); + for (let j = 0; j < ips.length; ++j) + isIPv4(ips[j]); } bench.end(n); } diff --git a/benchmark/net/net-is-ip-v6.js b/benchmark/net/net-is-ip-v6.js index 484d3b41c0002a..ee52339fec7048 100644 --- a/benchmark/net/net-is-ip-v6.js +++ b/benchmark/net/net-is-ip-v6.js @@ -3,9 +3,11 @@ const common = require('../common.js'); const { isIPv6 } = require('net'); -const minIPv6 = '::1'; -const maxIPv6 = 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'; -const invalid = '0.0.0.0'; +const ips = [ + '::1', + 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', + '0.0.0.0' +]; const bench = common.createBenchmark(main, { n: [1e7], @@ -14,9 +16,8 @@ const bench = common.createBenchmark(main, { function main({ n }) { bench.start(); for (let i = 0; i < n; ++i) { - isIPv6(minIPv6); - isIPv6(maxIPv6); - isIPv6(invalid); + for (let j = 0; j < ips.length; ++j) + isIPv6(ips[j]); } bench.end(n); } From d4675cf5801e22b258b792752f808bef6468a690 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Mon, 11 Sep 2023 01:41:36 +0200 Subject: [PATCH 5/5] add trailing comma --- benchmark/net/net-is-ip-v6.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmark/net/net-is-ip-v6.js b/benchmark/net/net-is-ip-v6.js index ee52339fec7048..dc12e215e42901 100644 --- a/benchmark/net/net-is-ip-v6.js +++ b/benchmark/net/net-is-ip-v6.js @@ -6,7 +6,7 @@ const { isIPv6 } = require('net'); const ips = [ '::1', 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', - '0.0.0.0' + '0.0.0.0', ]; const bench = common.createBenchmark(main, {