Skip to content

Commit a9eb86b

Browse files
committed
perf: Avoid non-greedy regex.
1 parent 64a6ce0 commit a9eb86b

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

src/BaseIRI.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { escapeRegex } from './Util';
33
// Do not handle base IRIs without scheme, and currently unsupported cases:
44
// - file: IRIs (which could also use backslashes)
55
// - IRIs containing /. or /.. or //
6-
const INVALID_OR_UNSUPPORTED = /^:?[^:?#]*(?:[?#]|$)|^file:|^[^:]*:\/*[^?#]+?\/(?:\.\.?(?:\/|$)|\/)/i;
6+
const BASE_UNSUPPORTED = /^:?[^:?#]*(?:[?#]|$)|^file:|^[^:]*:\/*[^?#]+?\/(?:\.\.?(?:\/|$)|\/)/i;
7+
const SUFFIX_SUPPORTED = /^(?:(?:[^/?#]{3,}|\.?[^/?#.]\.?)(?:\/[^/?#]{3,}|\.?[^/?#.]\.?)*\/?)?(?:[?#]|$)/;
78
const CURRENT = './';
89
const PARENT = '../';
910
const QUERY = '?';
@@ -18,7 +19,7 @@ export default class BaseIRI {
1819
}
1920

2021
static supports(base) {
21-
return !INVALID_OR_UNSUPPORTED.test(base);
22+
return !BASE_UNSUPPORTED.test(base);
2223
}
2324

2425
_getBaseMatcher() {
@@ -85,9 +86,7 @@ export default class BaseIRI {
8586
if (parentPath) {
8687
const suffix = iri.substring(length);
8788
// Don't abbreviate unsupported path
88-
if (parentPath !== QUERY &&
89-
/(?:^|\/)(?:\/|..?(?:[/#?]|$))/.test(suffix) && // fast test
90-
/^(?:[^#?]*?\/)?(?:\/|\.\.?(?:[/#?]|$))/.test(suffix)) // rigorous test
89+
if (parentPath !== QUERY && !SUFFIX_SUPPORTED.test(suffix))
9190
return iri;
9291
// Omit ./ with fragment or query string
9392
if (parentPath === CURRENT && /^[^?#]/.test(suffix))

0 commit comments

Comments
 (0)