remusao / tldts

JavaScript Library to extract domains, subdomains and public suffixes from complex URIs.
https://npmjs.com/tldts
MIT License
476 stars 20 forks source link

`parse` with only selected fields #2050

Closed SukkaW closed 3 months ago

SukkaW commented 4 months ago

In my use case, I only want to get the domain and the subdomain of a domain.

I can simply do tldts.getDomain() and then tldts.getSubdomain(), but the input domain would be parsed twice.

Under the hood, tldts-core does have FLAG constants, but it is not exposed to the tldts. On the other hand, the suffixLookup function is not exported so I can't build my own parseDomainAndSubdomain on tldts-core directly.

remusao commented 3 months ago

Hi @SukkaW,

At the moment this is not possible but I wanted to point out that in your use-case, the only overhead you have is the call to getDomainWithoutSuffix which consist in a single call to .slice. This should be very fast and low-overhead. Is that a problem for your use-case?

Best,

SukkaW commented 3 months ago

@remusao

If I call both getDomain and getSubdomain on the same domain, anything before FLAG.SUB_DOMAIN will be called twice:

At the moment this is not possible but I wanted to point out that in your use-case, the only overhead you have is the call to getDomainWithoutSuffix which consist in a single call to .slice. This should be very fast and low-overhead. Is that a problem for your use-case?

Yes, replacing the getDomain and getSubdomain with one single parse call does improve the performance by about 30%, thanks!

remusao commented 3 months ago

Sounds good. I'll close this as resolved but feel free to open an issue if you'd need any other information of help.