Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(parse): remove additional slice for quoted values #143

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this change for performance, but I think this changes the behavior. Before it trimmed whitespace around potential quotes, but now it'd trim whitespace within quotes.

: str.slice(eqIdx, endIdx).trim()

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