Skip to content

Commit

Permalink
perf(parse): remove additional slice for quoted values
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtextrem committed Jul 14, 2022
1 parent 663c9ae commit fbe7d1c
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,17 @@ function parse(str, options) {
var dec = opt.decode || decode;

var index = 0
var eqIdx = 0
var endIdx = 0
while (index < str.length) {
var eqIdx = str.indexOf('=', index)
eqIdx = str.indexOf('=', index)

// no more cookie pairs
if (eqIdx === -1) {
break
}

var endIdx = str.indexOf(';', index)
endIdx = str.indexOf(';', index)

if (endIdx === -1) {
endIdx = str.length
Expand All @@ -72,16 +74,13 @@ function parse(str, options) {
continue
}

var key = str.slice(index, eqIdx).trim()
var key = str.slice(index, eqIdx++).trim()

// only assign once
if (undefined === obj[key]) {
var val = str.slice(eqIdx + 1, endIdx).trim()

// quoted values
if (val.charCodeAt(0) === 0x22) {
val = val.slice(1, -1)
}
var val = (str.charCodeAt(eqIdx) === 0x22)
? str.slice(eqIdx + 1, endIdx - 1).trim()
: str.slice(eqIdx, endIdx).trim()

obj[key] = tryDecode(val, dec);
}
Expand Down

0 comments on commit fbe7d1c

Please sign in to comment.