Repository navigation
Commit 2d72298
feat(helpers): read the declared protocol instead of resolving it (#864)
* perf(helpers): read protocol without a thrown URL construction
`protocol` resolved the scheme through `new URL` inside a try/catch, so every
schemeless value paid for a constructed and thrown exception to arrive at ''.
Relative hrefs are the common input for callers walking a document's links.
A value with no scheme can never parse without a base, so it resolves to ''
without touching the URL parser. Anything carrying a scheme still goes through
`new URL`, which keeps the cases where parsing the whole URL is what decides the
answer: `new URL('http://')` throws, so `protocol('http://')` stays ''.
100k calls, best of 3:
relative hrefs 299.3ms -> 1.9ms 155x
mixed 162.2ms -> 8.5ms 19x
absolute http 14.2ms -> 15.4ms 0.9x
Inputs that do carry a scheme pay one extra regex test, about 8% on a ~140ns
call, since the URL construction happens either way.
Output is unchanged. Verified by differential fuzz against the previous
implementation over 200,074 inputs, including leading control characters, tabs
and newlines inside the scheme, non-strings, and URLs the parser rejects
outright: no mismatches.
* feat(helpers): read the declared protocol instead of resolving it
`protocol` resolved the scheme through `new URL` inside a try/catch, so a value
without a scheme constructed a URL, threw, and got caught on the way to ''.
Relative hrefs are the common input for callers walking a document's links, which
put the exception on the hot path.
Which scheme a value declares is a lexical question, so it is now read directly.
100k calls, best of 3:
relative hrefs 299.4ms -> 2.6ms 116x
mixed 157.1ms -> 3.6ms 44x
absolute http 14.7ms -> 4.0ms 3.7x
non-http schemes 13.5ms -> 4.3ms 3.2x
The scheme is read after the cleanup the URL parser applies before reading it
itself: a leading C0 control or space is skipped and every ASCII tab or newline is
removed, so ' mai\tlto:a@b' still declares `mailto`.
Output changes for one class of input: a URL whose authority the parser rejects.
protocol('http://') '' -> 'http'
protocol('http://[') '' -> 'http'
protocol('foo://[') '' -> 'foo'
'' described the whole URL rather than the scheme sitting in front of it. Whether
a URL is usable is what `isUrl` answers.
Verified by differential fuzz against the previous implementation over 300,079
inputs, including leading control characters, tabs and newlines inside the scheme,
and non-strings: 300,070 identical, 9 divergences, every one of them the class
above, and none outside it.
* perf(helpers): read the protocol in a single scan
The regex ran over a value that a trim loop and three `includes` scans had
already walked. One charCode pass answers it, with the tab/newline removal
demoted to the rare path that needs it.
100k calls per set, best of 5:
| input | before | after | |
|-------------------|--------|-------|-------|
| relative hrefs | 2.52ms | 0.80ms| 3.2x |
| mixed | 3.20ms | 1.25ms| 2.6x |
| absolute http | 4.64ms | 1.89ms| 2.5x |
| non-http schemes | 4.27ms | 1.80ms| 2.4x |
| tabs / leading ws | 8.52ms | 6.58ms| 1.3x |
Differential fuzz against the previous implementation over 500,044 inputs:
0 mismatches.
`normalizeUrl` asked the same question through `new URL`, so it now reads
the helper and drops a URL construction per call.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_014CoQ8BQTgwWhjmLpS8jqTE
* perf(helpers): inline the scheme character classes
V8 was not inlining `isSchemeHead`/`isSchemeChar`, and `toLowerCase` was
called on schemes that were already lowercase. Both are now decided in the
loop that is already reading the characters.
100k calls per set, best of 9, against a noise floor of +/-15% measured with
a byte-identical clone of the previous body:
| input | before | after | |
|------------------|--------|--------|-------|
| mixed | 1.56ms | 1.16ms | -25% |
| absolute http | 1.88ms | 1.48ms | -22% |
| non-http schemes | 1.79ms | 1.42ms | -21% |
Differential fuzz against both earlier implementations over 500,044 inputs:
0 mismatches.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_014CoQ8BQTgwWhjmLpS8jqTE
* refactor(helpers): drop the single-use temporaries in protocol
`isLower` was an alias for the flag's initial value and `isDigit` named a
class that every neighbouring line inlines. Measured against the previous
body: within the +/-15% noise floor on every input class.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_014CoQ8BQTgwWhjmLpS8jqTE
* test(helpers): give the strip comment a subject
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_014CoQ8BQTgwWhjmLpS8jqTE
---------
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>1 parent e66c71f commit 2d72298
2 files changed
Lines changed: 75 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
123 | 123 | | |
124 | 124 | | |
125 | 125 | | |
| 126 | + | |
| 127 | + | |
126 | 128 | | |
127 | 129 | | |
128 | 130 | | |
| |||
143 | 145 | | |
144 | 146 | | |
145 | 147 | | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
146 | 179 | | |
147 | 180 | | |
148 | 181 | | |
| |||
156 | 189 | | |
157 | 190 | | |
158 | 191 | | |
159 | | - | |
| 192 | + | |
160 | 193 | | |
161 | 194 | | |
162 | 195 | | |
| |||
205 | 238 | | |
206 | 239 | | |
207 | 240 | | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | 241 | | |
214 | 242 | | |
215 | 243 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| 30 | + | |
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
| |||
64 | 65 | | |
65 | 66 | | |
66 | 67 | | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
67 | 108 | | |
68 | 109 | | |
69 | 110 | | |
| |||
0 commit comments