From 223e37db7db01d412bd846d55075c538da9dfdce Mon Sep 17 00:00:00 2001 From: Khafra Date: Tue, 14 Feb 2023 21:58:16 -0500 Subject: [PATCH] perf: improve cookie parsing performance (#1931) --- lib/cookies/parse.js | 16 ++++++++-------- lib/fetch/dataURL.js | 1 + 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/cookies/parse.js b/lib/cookies/parse.js index 6a1e37be6b7..aae2750360b 100644 --- a/lib/cookies/parse.js +++ b/lib/cookies/parse.js @@ -2,7 +2,7 @@ const { maxNameValuePairSize, maxAttributeValueSize } = require('./constants') const { isCTLExcludingHtab } = require('./util') -const { collectASequenceOfCodePoints } = require('../fetch/dataURL') +const { collectASequenceOfCodePointsFast } = require('../fetch/dataURL') const assert = require('assert') /** @@ -32,7 +32,7 @@ function parseSetCookie (header) { // (including the %x3B (";") in question). const position = { position: 0 } - nameValuePair = collectASequenceOfCodePoints((char) => char !== ';', header, position) + nameValuePair = collectASequenceOfCodePointsFast(';', header, position) unparsedAttributes = header.slice(position.position) } else { // Otherwise: @@ -54,8 +54,8 @@ function parseSetCookie (header) { // empty) value string consists of the characters after the first // %x3D ("=") character. const position = { position: 0 } - name = collectASequenceOfCodePoints( - (char) => char !== '=', + name = collectASequenceOfCodePointsFast( + '=', nameValuePair, position ) @@ -106,8 +106,8 @@ function parseUnparsedAttributes (unparsedAttributes, cookieAttributeList = {}) if (unparsedAttributes.includes(';')) { // 1. Consume the characters of the unparsed-attributes up to, but // not including, the first %x3B (";") character. - cookieAv = collectASequenceOfCodePoints( - (char) => char !== ';', + cookieAv = collectASequenceOfCodePointsFast( + ';', unparsedAttributes, { position: 0 } ) @@ -134,8 +134,8 @@ function parseUnparsedAttributes (unparsedAttributes, cookieAttributeList = {}) // character. const position = { position: 0 } - attributeName = collectASequenceOfCodePoints( - (char) => char !== '=', + attributeName = collectASequenceOfCodePointsFast( + '=', cookieAv, position ) diff --git a/lib/fetch/dataURL.js b/lib/fetch/dataURL.js index c950167d295..0d4a46956db 100644 --- a/lib/fetch/dataURL.js +++ b/lib/fetch/dataURL.js @@ -556,6 +556,7 @@ module.exports = { dataURLProcessor, URLSerializer, collectASequenceOfCodePoints, + collectASequenceOfCodePointsFast, stringPercentDecode, parseMIMEType, collectAnHTTPQuotedString,