Skip to content

Commit

Permalink
perf: use collectASequenceOfCodePointsFast
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev committed Feb 26, 2023
1 parent 588d62d commit e21de08
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
4 changes: 3 additions & 1 deletion lib/fetch/dataURL.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,11 @@ function collectASequenceOfCodePoints (condition, input, position) {

/**
* A faster collectASequenceOfCodePoints that only works when comparing a single character.
* @template {string|Buffer} T
* @param {string} char
* @param {string} input
* @param {T} input
* @param {{ position: number }} position
* @returns {T}
*/
function collectASequenceOfCodePointsFast (char, input, position) {
const idx = input.indexOf(char, position.position)
Expand Down
13 changes: 7 additions & 6 deletions lib/formdata/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const { TextSearch } = require('./textsearch')
const {
parseMIMEType,
collectASequenceOfCodePoints,
collectASequenceOfCodePointsFast,
stringPercentDecode
} = require('../fetch/dataURL')

Expand Down Expand Up @@ -519,8 +520,8 @@ class FormDataParser extends Writable {
const position = { position: 0 }

while (position.position < buffer.length) {
const nameBuffer = collectASequenceOfCodePoints(
(char) => char !== chars[':'],
const nameBuffer = collectASequenceOfCodePointsFast(
chars[':'],
buffer,
position
)
Expand Down Expand Up @@ -557,8 +558,8 @@ class FormDataParser extends Writable {

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

if (name !== 'content-disposition') {
collectASequenceOfCodePoints(
(char) => char !== chars.cr && buffer[position.position + 1] !== chars.lf,
collectASequenceOfCodePointsFast(
'\r\n',
buffer,
position
)
Expand Down
6 changes: 3 additions & 3 deletions lib/formdata/util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { collectASequenceOfCodePoints } = require('../fetch/dataURL')
const { collectASequenceOfCodePointsFast } = require('../fetch/dataURL')

/**
* Collect a string inside of quotes. Unlike
Expand All @@ -13,8 +13,8 @@ function collectHTTPQuotedStringLenient (str) {
return str
}

return collectASequenceOfCodePoints(
(char) => char.charCodeAt(0) !== 0x22, // "
return collectASequenceOfCodePointsFast(
'"',
str,
{ position: 1 }
)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"build:wasm": "node build/wasm.js --docker",
"lint": "standard | snazzy",
"lint:fix": "standard --fix | snazzy",
"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",
"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",
"test:busboy": "node scripts/verifyVersion 18 || node test/busboy/test.js",
"test:cookies": "node scripts/verifyVersion 16 || tap test/cookie/*.js",
"test:node-fetch": "node scripts/verifyVersion.js 16 || mocha test/node-fetch",
Expand Down

0 comments on commit e21de08

Please sign in to comment.