Skip to content
Merged
Show file tree
Hide file tree
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
31 changes: 16 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export function parseCookie(str: string, options?: ParseOptions): Cookies {

do {
const eqIdx = eqIndex(str, index, len);
if (eqIdx === -1) break; // No more cookie pairs.
if (eqIdx === len) break; // No more cookie pairs.

const endIdx = endIndex(str, index, len);

Expand Down Expand Up @@ -398,24 +398,25 @@ export function parseSetCookie(str: string, options?: ParseOptions): SetCookie {
const dec = options?.decode || decode;
const len = str.length;
const endIdx = endIndex(str, 0, len);
const eqIdx = eqIndex(str, 0, endIdx);
let eqIdx = eqIndex(str, 0, len);
const setCookie: SetCookie =
eqIdx === -1
? { name: "", value: dec(valueSlice(str, 0, endIdx)) }
: {
eqIdx < endIdx

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

All these are mostly reversing the conditions instead of using >= purely to shave 4 bytes in the compressed output 😅

? {
name: valueSlice(str, 0, eqIdx),
value: dec(valueSlice(str, eqIdx + 1, endIdx)),
};
}
: { name: "", value: dec(valueSlice(str, 0, endIdx)) };

let index = endIdx + 1;
while (index < len) {
const endIdx = endIndex(str, index, len);
const eqIdx = eqIndex(str, index, endIdx);
if (eqIdx < index) eqIdx = eqIndex(str, index, len);

const attr =
eqIdx === -1
? valueSlice(str, index, endIdx)
: valueSlice(str, index, eqIdx);
const val = eqIdx === -1 ? undefined : valueSlice(str, eqIdx + 1, endIdx);
eqIdx < endIdx
? valueSlice(str, index, eqIdx)
: valueSlice(str, index, endIdx);
const val = eqIdx < endIdx ? valueSlice(str, eqIdx + 1, endIdx) : undefined;

switch (attr.toLowerCase()) {
case "httponly":
Expand Down Expand Up @@ -472,19 +473,19 @@ export function parseSetCookie(str: string, options?: ParseOptions): SetCookie {
}

/**
* Find the `;` character between `min` and `len` in str.
* Find the next `;` character, or return `len`.
*/
function endIndex(str: string, min: number, len: number) {
const index = str.indexOf(";", min);
return index === -1 ? len : index;
}

/**
* Find the `=` character between `min` and `max` in str.
* Find the next `=` character, or return `len`.
*/
function eqIndex(str: string, min: number, max: number) {
function eqIndex(str: string, min: number, len: number) {
const index = str.indexOf("=", min);
return index < max ? index : -1;
return index === -1 ? len : index;
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/parse-cookie.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ describe("cookie.parseCookie", () => {
bench("100 cookies", () => {
cookie.parseCookie(cookies100);
});

const semicolons = ";".repeat(100_000);
bench("100k semicolons", () => {
cookie.parseCookie(semicolons);
});
});

describe("parse top-sites", () => {
Expand Down
5 changes: 5 additions & 0 deletions src/parse-set-cookie.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ describe("parse top-sites", () => {
}
});
});

const semicolons = ";".repeat(100_000);
bench("100k semicolons", () => {
cookie.parseSetCookie(semicolons);
});
});
Loading