Skip to content

Commit 46e3d7d

Browse files
authored
perf(parse): remove additional slice for quoted values
see fastify/fastify-cookie#194 (comment)
1 parent 663c9ae commit 46e3d7d

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

index.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,17 @@ function parse(str, options) {
5454
var dec = opt.decode || decode;
5555

5656
var index = 0
57+
var eqIdx = 0
58+
var endIdx = 0
5759
while (index < str.length) {
58-
var eqIdx = str.indexOf('=', index)
60+
eqIdx = str.indexOf('=', index)
5961

6062
// no more cookie pairs
6163
if (eqIdx === -1) {
6264
break
6365
}
6466

65-
var endIdx = str.indexOf(';', index)
67+
endIdx = str.indexOf(';', index)
6668

6769
if (endIdx === -1) {
6870
endIdx = str.length
@@ -72,16 +74,13 @@ function parse(str, options) {
7274
continue
7375
}
7476

75-
var key = str.slice(index, eqIdx).trim()
77+
var key = str.slice(index, eqIdx++).trim()
7678

7779
// only assign once
7880
if (undefined === obj[key]) {
79-
var val = str.slice(eqIdx + 1, endIdx).trim()
80-
81-
// quoted values
82-
if (val.charCodeAt(0) === 0x22) {
83-
val = val.slice(1, -1)
84-
}
81+
var val = (str.charCodeAt(eqIdx) === 0x22)
82+
? str.slice(eqIdx + 1, terminatorPos - 1).trim()
83+
: str.slice(eqIdx, terminatorPos).trim()
8584

8685
obj[key] = tryDecode(val, dec);
8786
}

0 commit comments

Comments
 (0)