From b2289dad6395b9d779179749b668d4a666766ab9 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Tue, 14 Feb 2023 15:33:03 -0500 Subject: [PATCH] url: improve performance by removing host PR-URL: https://github.com/nodejs/node/pull/46547 Reviewed-By: James M Snell Reviewed-By: Filip Skokan Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: Rich Trott --- lib/internal/url.js | 8 +++---- src/node_url.cc | 21 ++++++++----------- .../test-whatwg-url-custom-inspect.js | 1 - 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/lib/internal/url.js b/lib/internal/url.js index 3ed22331cba399..23626595ee78e2 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -145,7 +145,6 @@ class URLContext { href = ''; origin = ''; protocol = ''; - host = ''; hostname = ''; pathname = ''; search = ''; @@ -634,14 +633,13 @@ class URL { return constructHref(this[context], options); } - #onParseComplete = (href, origin, protocol, host, hostname, pathname, + #onParseComplete = (href, origin, protocol, hostname, pathname, search, username, password, port, hash, hasHost, hasOpaquePath) => { const ctx = this[context]; ctx.href = href; ctx.origin = origin; ctx.protocol = protocol; - ctx.host = host; ctx.hostname = hostname; ctx.pathname = pathname; ctx.search = search; @@ -724,7 +722,9 @@ class URL { get host() { if (!isURLThis(this)) throw new ERR_INVALID_THIS('URL'); - return this[context].host; + const port = this[context].port; + const suffix = port.length > 0 ? `:${port}` : ''; + return this[context].hostname + suffix; } set host(value) { diff --git a/src/node_url.cc b/src/node_url.cc index 34e8d96ba3b931..4a7bbd71a44dbc 100644 --- a/src/node_url.cc +++ b/src/node_url.cc @@ -52,16 +52,15 @@ void SetArgs(Environment* env, Local argv[12], const ada::result& url) { argv[0] = Utf8String(isolate, url->get_href()); argv[1] = Utf8String(isolate, url->get_origin()); argv[2] = Utf8String(isolate, url->get_protocol()); - argv[3] = Utf8String(isolate, url->get_host()); - argv[4] = Utf8String(isolate, url->get_hostname()); - argv[5] = Utf8String(isolate, url->get_pathname()); - argv[6] = Utf8String(isolate, url->get_search()); - argv[7] = Utf8String(isolate, url->get_username()); - argv[8] = Utf8String(isolate, url->get_password()); - argv[9] = Utf8String(isolate, url->get_port()); - argv[10] = Utf8String(isolate, url->get_hash()); - argv[11] = Boolean::New(isolate, url->host.has_value()); - argv[12] = Boolean::New(isolate, url->has_opaque_path); + argv[3] = Utf8String(isolate, url->get_hostname()); + argv[4] = Utf8String(isolate, url->get_pathname()); + argv[5] = Utf8String(isolate, url->get_search()); + argv[6] = Utf8String(isolate, url->get_username()); + argv[7] = Utf8String(isolate, url->get_password()); + argv[8] = Utf8String(isolate, url->get_port()); + argv[9] = Utf8String(isolate, url->get_hash()); + argv[10] = Boolean::New(isolate, url->host.has_value()); + argv[11] = Boolean::New(isolate, url->has_opaque_path); } void Parse(const FunctionCallbackInfo& args) { @@ -108,7 +107,6 @@ void Parse(const FunctionCallbackInfo& args) { undef, undef, undef, - undef, }; SetArgs(env, argv, out); USE(success_callback_->Call( @@ -259,7 +257,6 @@ void UpdateUrl(const FunctionCallbackInfo& args) { undef, undef, undef, - undef, }; SetArgs(env, argv, out); USE(success_callback_->Call( diff --git a/test/parallel/test-whatwg-url-custom-inspect.js b/test/parallel/test-whatwg-url-custom-inspect.js index e64f9991f5f0d9..ad7a48f72d5958 100644 --- a/test/parallel/test-whatwg-url-custom-inspect.js +++ b/test/parallel/test-whatwg-url-custom-inspect.js @@ -49,7 +49,6 @@ assert.strictEqual( href: 'https://username:password@host.name:8080/path/name/?que=ry#hash', origin: 'https://host.name:8080', protocol: 'https:', - host: 'host.name:8080', hostname: 'host.name', pathname: '/path/name/', search: '?que=ry',