Skip to content

Commit

Permalink
perf: avoid domainToAscii on pure lowercase ascii cases (#67)
Browse files Browse the repository at this point in the history
* perf: avoid domainToAscii on pure cases

* use fn instead of re
  • Loading branch information
Uzlopak committed Aug 18, 2023
1 parent b119a8c commit 51e27a0
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,19 @@ function serialize (cmpts, opts) {
return uriTokens.join('')
}

const hexLookUp = Array.from({ length: 127 }, (v, k) => /[^!"$&'()*+,.;=_`a-z{}~-]/.test(String.fromCharCode(k)))

function nonSimpleDomain (value) {
let code = 0
for (let i = 0, len = value.length; i < len; ++i) {
code = value.charCodeAt(i)
if (code > 126 || hexLookUp[code]) {
return true
}
}
return false
}

const URI_PARSE = /^(?:([^:/?#]+):)?(?:\/\/((?:([^/?#@]*)@)?(\[[^/?#\]]+\]|[^/?#:]*)(?::(\d*))?))?([^?#]*)(?:\?([^#]*))?(?:#((?:.|\n|\r)*))?/i

function parse (uri, opts) {
Expand Down Expand Up @@ -239,7 +252,7 @@ function parse (uri, opts) {
// check if scheme can't handle IRIs
if (!options.unicodeSupport && (!schemeHandler || !schemeHandler.unicodeSupport)) {
// if host component is a domain name
if (parsed.host && (options.domainHost || (schemeHandler && schemeHandler.domainHost))) {
if (parsed.host && (options.domainHost || (schemeHandler && schemeHandler.domainHost)) && nonSimpleDomain(parsed.host)) {
// convert Unicode IDN -> ASCII IDN
try {
parsed.host = URL.domainToASCII(parsed.host.toLowerCase())
Expand Down

0 comments on commit 51e27a0

Please sign in to comment.