perf: improve parse identifiers (#20184)

This commit is contained in:
Alexander Akait 2025-12-01 19:37:46 +03:00 committed by GitHub
parent 04375e8fca
commit 728ddb77cb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 87 additions and 17 deletions

View File

@ -0,0 +1,5 @@
---
"@fake-scope/fake-pkg": patch
---
The speed of identifier parsing has been improved

View File

@ -321,6 +321,7 @@ const absolutify = makeCacheableWithContext(_absolutify);
const PATH_QUERY_FRAGMENT_REGEXP =
/^((?:\0.|[^?#\0])*)(\?(?:\0.|[^#\0])*)?(#.*)?$/;
const PATH_QUERY_REGEXP = /^((?:\0.|[^?\0])*)(\?.*)?$/;
const ZERO_ESCAPE_REGEXP = /\0(.)/g;
/** @typedef {{ resource: string, path: string, query: string, fragment: string }} ParsedResource */
/** @typedef {{ resource: string, path: string, query: string }} ParsedResourceWithoutFragment */
@ -330,15 +331,56 @@ const PATH_QUERY_REGEXP = /^((?:\0.|[^?\0])*)(\?.*)?$/;
* @returns {ParsedResource} parsed parts
*/
const _parseResource = (str) => {
const match =
/** @type {[string, string, string | undefined, string | undefined]} */
(/** @type {unknown} */ (PATH_QUERY_FRAGMENT_REGEXP.exec(str)));
return {
resource: str,
path: match[1].replace(/\0(.)/g, "$1"),
query: match[2] ? match[2].replace(/\0(.)/g, "$1") : "",
fragment: match[3] || ""
};
const firstEscape = str.indexOf("\0");
// Handle `\0`
if (firstEscape !== -1) {
const match =
/** @type {[string, string, string | undefined, string | undefined]} */
(/** @type {unknown} */ (PATH_QUERY_FRAGMENT_REGEXP.exec(str)));
return {
resource: str,
path: match[1].replace(ZERO_ESCAPE_REGEXP, "$1"),
query: match[2] ? match[2].replace(ZERO_ESCAPE_REGEXP, "$1") : "",
fragment: match[3] || ""
};
}
/** @type {ParsedResource} */
const result = { resource: str, path: "", query: "", fragment: "" };
const queryStart = str.indexOf("?");
const fragmentStart = str.indexOf("#");
if (fragmentStart < 0) {
if (queryStart < 0) {
result.path = result.resource;
// No fragment, no query
return result;
}
result.path = str.slice(0, queryStart);
result.query = str.slice(queryStart);
// Query, no fragment
return result;
}
if (queryStart < 0 || fragmentStart < queryStart) {
result.path = str.slice(0, fragmentStart);
result.fragment = str.slice(fragmentStart);
// Fragment, no query
return result;
}
result.path = str.slice(0, queryStart);
result.query = str.slice(queryStart, fragmentStart);
result.fragment = str.slice(fragmentStart);
// Query and fragment
return result;
};
/**
@ -347,14 +389,37 @@ const _parseResource = (str) => {
* @returns {ParsedResourceWithoutFragment} parsed parts
*/
const _parseResourceWithoutFragment = (str) => {
const match =
/** @type {[string, string, string | undefined]} */
(/** @type {unknown} */ (PATH_QUERY_REGEXP.exec(str)));
return {
resource: str,
path: match[1].replace(/\0(.)/g, "$1"),
query: match[2] ? match[2].replace(/\0(.)/g, "$1") : ""
};
const firstEscape = str.indexOf("\0");
// Handle `\0`
if (firstEscape !== -1) {
const match =
/** @type {[string, string, string | undefined]} */
(/** @type {unknown} */ (PATH_QUERY_REGEXP.exec(str)));
return {
resource: str,
path: match[1].replace(ZERO_ESCAPE_REGEXP, "$1"),
query: match[2] ? match[2].replace(ZERO_ESCAPE_REGEXP, "$1") : ""
};
}
/** @type {ParsedResourceWithoutFragment} */
const result = { resource: str, path: "", query: "" };
const queryStart = str.indexOf("?");
if (queryStart < 0) {
result.path = result.resource;
// No query
return result;
}
result.path = str.slice(0, queryStart);
result.query = str.slice(queryStart);
// Query
return result;
};
/**