Skip to content

Commit 1340068

Browse files
committed
perf(http-request): hoist regex literals out of hot paths
Extract three regex patterns to module-scope constants: - CHECKSUM_BSD_RE + CHECKSUM_GNU_RE: were recompiled once per line inside parseChecksums()'s loop (V8 caches them, but explicit hoist is clearer intent and sidesteps future engine changes) - RETRY_AFTER_INT_RE: was a literal inside parseRetryAfterHeader() Low-impact change — V8's regex literal cache usually handles this — but the hoisted constants make the intent explicit and give the patterns searchable names for future grep/audit.
1 parent c441c8d commit 1340068

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

src/http-request.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,6 +1790,10 @@ export async function httpText(
17901790
* console.log(checksums['file.zip']) // 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
17911791
* ```
17921792
*/
1793+
const CHECKSUM_BSD_RE = /^SHA256\s+\((.+)\)\s+=\s+([a-fA-F0-9]{64})$/
1794+
const CHECKSUM_GNU_RE = /^([a-fA-F0-9]{64})\s+(.+)$/
1795+
const RETRY_AFTER_INT_RE = /^\d+$/
1796+
17931797
export function parseChecksums(text: string): Checksums {
17941798
const checksums: Checksums = { __proto__: null } as unknown as Checksums
17951799

@@ -1800,16 +1804,14 @@ export function parseChecksums(text: string): Checksums {
18001804
}
18011805

18021806
// Try BSD style: "SHA256 (filename) = hash"
1803-
const bsdMatch = trimmed.match(
1804-
/^SHA256\s+\((.+)\)\s+=\s+([a-fA-F0-9]{64})$/,
1805-
)
1807+
const bsdMatch = CHECKSUM_BSD_RE.exec(trimmed)
18061808
if (bsdMatch) {
18071809
checksums[bsdMatch[1]!] = bsdMatch[2]!.toLowerCase()
18081810
continue
18091811
}
18101812

18111813
// Try GNU/simple style: "hash filename" or "hash filename"
1812-
const gnuMatch = trimmed.match(/^([a-fA-F0-9]{64})\s+(.+)$/)
1814+
const gnuMatch = CHECKSUM_GNU_RE.exec(trimmed)
18131815
if (gnuMatch) {
18141816
checksums[gnuMatch[2]!] = gnuMatch[1]!.toLowerCase()
18151817
}
@@ -1851,7 +1853,7 @@ export function parseRetryAfterHeader(
18511853
}
18521854
// Try parsing as seconds (strict integer — reject partial like "10abc").
18531855
const trimmed = raw.trim()
1854-
if (/^\d+$/.test(trimmed)) {
1856+
if (RETRY_AFTER_INT_RE.test(trimmed)) {
18551857
const seconds = Number(trimmed)
18561858
return seconds * 1000
18571859
}

0 commit comments

Comments
 (0)