Skip to content

Commit e21de08

Browse files
committed
perf: use collectASequenceOfCodePointsFast
1 parent 588d62d commit e21de08

File tree

4 files changed

+14
-11
lines changed

4 files changed

+14
-11
lines changed

lib/fetch/dataURL.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,11 @@ function collectASequenceOfCodePoints (condition, input, position) {
168168

169169
/**
170170
* A faster collectASequenceOfCodePoints that only works when comparing a single character.
171+
* @template {string|Buffer} T
171172
* @param {string} char
172-
* @param {string} input
173+
* @param {T} input
173174
* @param {{ position: number }} position
175+
* @returns {T}
174176
*/
175177
function collectASequenceOfCodePointsFast (char, input, position) {
176178
const idx = input.indexOf(char, position.position)

lib/formdata/parser.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const { TextSearch } = require('./textsearch')
1414
const {
1515
parseMIMEType,
1616
collectASequenceOfCodePoints,
17+
collectASequenceOfCodePointsFast,
1718
stringPercentDecode
1819
} = require('../fetch/dataURL')
1920

@@ -519,8 +520,8 @@ class FormDataParser extends Writable {
519520
const position = { position: 0 }
520521

521522
while (position.position < buffer.length) {
522-
const nameBuffer = collectASequenceOfCodePoints(
523-
(char) => char !== chars[':'],
523+
const nameBuffer = collectASequenceOfCodePointsFast(
524+
chars[':'],
524525
buffer,
525526
position
526527
)
@@ -557,8 +558,8 @@ class FormDataParser extends Writable {
557558

558559
// Go to the next header if one is available. Headers are split by \r\n,
559560
// so we skip all characters until the sequence is found.
560-
collectASequenceOfCodePoints(
561-
(char) => char !== chars.cr && buffer[position.position + 1] !== chars.lf,
561+
collectASequenceOfCodePointsFast(
562+
'\r\n',
562563
buffer,
563564
position
564565
)
@@ -595,8 +596,8 @@ class FormDataParser extends Writable {
595596
}
596597

597598
if (name !== 'content-disposition') {
598-
collectASequenceOfCodePoints(
599-
(char) => char !== chars.cr && buffer[position.position + 1] !== chars.lf,
599+
collectASequenceOfCodePointsFast(
600+
'\r\n',
600601
buffer,
601602
position
602603
)

lib/formdata/util.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const { collectASequenceOfCodePoints } = require('../fetch/dataURL')
3+
const { collectASequenceOfCodePointsFast } = require('../fetch/dataURL')
44

55
/**
66
* Collect a string inside of quotes. Unlike
@@ -13,8 +13,8 @@ function collectHTTPQuotedStringLenient (str) {
1313
return str
1414
}
1515

16-
return collectASequenceOfCodePoints(
17-
(char) => char.charCodeAt(0) !== 0x22, // "
16+
return collectASequenceOfCodePointsFast(
17+
'"',
1818
str,
1919
{ position: 1 }
2020
)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"build:wasm": "node build/wasm.js --docker",
4747
"lint": "standard | snazzy",
4848
"lint:fix": "standard --fix | snazzy",
49-
"test": "npm run test:tap && npm run test:node-fetch && npm run test:fetch && npm run test:cookies && npm run test:wpt && npm run test:websocket && npm run test:jest && tsd",
49+
"test": "npm run test:tap && npm run test:node-fetch && npm run test:fetch && npm run test:cookies && npm run test:busboy && npm run test:wpt && npm run test:websocket && npm run test:jest && tsd",
5050
"test:busboy": "node scripts/verifyVersion 18 || node test/busboy/test.js",
5151
"test:cookies": "node scripts/verifyVersion 16 || tap test/cookie/*.js",
5252
"test:node-fetch": "node scripts/verifyVersion.js 16 || mocha test/node-fetch",

0 commit comments

Comments
 (0)