From 52ca3c9992eb3c1df93748fe2defd9ba297156fb Mon Sep 17 00:00:00 2001 From: SukkaW Date: Mon, 16 Oct 2023 16:20:55 +0800 Subject: [PATCH] perf: improve `tldts.getDomain` speed --- src/common/tld.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/common/tld.js b/src/common/tld.js index 5c9eb6fd73..e428560f5c 100644 --- a/src/common/tld.js +++ b/src/common/tld.js @@ -4,7 +4,19 @@ import { getDomain as getDomain_, getPublicSuffix as getPublicSuffix_ } from 'tl * tldts does not respect the public suffix list by default, but can be opt in manually * with the option `allowPrivateDomains`. Hoist the `sharedOpts` can also help avoid * re-creating the object every time. + * + * Note `extractHostname` and `validateHostname` are set to false because the inputs are + * from `new URL(url).hostname` and are known to be valid */ -const sharedOpts = { allowPrivateDomains: true }; -export const getDomain = url => getDomain_(url, sharedOpts); -export const getPublicSuffix = url => getPublicSuffix_(url, sharedOpts); +const getDomainSharedOpts = { + allowPrivateDomains: true, + extractHostname: false, // inputs are already hostnames + validateHostname: false, // inputs are already valid, no need to perform extra validation +}; + +const getPublicSuffixSharedOpts = { + allowPrivateDomains: true +}; + +export const getDomain = url => getDomain_(url, getDomainSharedOpts); +export const getPublicSuffix = url => getPublicSuffix_(url, getPublicSuffixSharedOpts);