Skip to content

Commit 6c95239

Browse files
committed
perf(urlencoded): optimize parameter counting for better memory efficiency
The previous implementation used �ody.split('&') which always processed the entire request body and allocated a full array, regardless of the parameter limit. The new implementation: - Counts '&' characters iteratively without array allocation - Exits immediately when the limit is reached - Handles edge case of empty/null body - Reduces time complexity from O(n) worst-case always to O(min(n, limit)) This particularly improves resilience against malicious requests with thousands of parameters attempting to exhaust server resources
1 parent 2f27257 commit 6c95239

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

lib/types/urlencoded.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,14 @@ function createQueryParser (options) {
131131
*/
132132

133133
function parameterCount (body, limit) {
134-
var len = body.split('&').length
134+
if (!body) return 0
135135

136-
return len > limit ? undefined : len - 1
136+
let count = 0
137+
for (const char of body) {
138+
if (char === '&') {
139+
if (++count >= limit) return undefined
140+
}
141+
}
142+
143+
return count
137144
}

0 commit comments

Comments
 (0)