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
11 changes: 7 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,11 @@ export function stringifyCookie(
options?: StringifyOptions,
): string {
const enc = options?.encode || encodeURIComponent;
const cookieStrings: string[] = [];
const keys = Object.keys(cookie);
let str = "";

for (const name of Object.keys(cookie)) {
for (let i = 0; i < keys.length; i++) {
const name = keys[i];
const val = cookie[name];
if (val === undefined) continue;

Expand All @@ -174,10 +176,11 @@ export function stringifyCookie(
throw new TypeError(`cookie val is invalid: ${val}`);
}

cookieStrings.push(`${name}=${value}`);
if (i > 0) str += "; ";
str += name + "=" + value;
}

return cookieStrings.join("; ");
return str;
}

/**
Expand Down
41 changes: 41 additions & 0 deletions src/stringify-cookie.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { describe, bench } from "vitest";
import * as cookie from "./index.js";

describe("cookie.stringifyCookie", () => {
bench("empty", () => {
cookie.stringifyCookie({});
});

bench("simple", () => {
cookie.stringifyCookie({ foo: "bar" });
});

bench("undefined values", () => {
cookie.stringifyCookie({
foo: "bar",
baz: undefined,
qux: "quux",
zap: undefined,
});
});

const cookies10 = genCookies(10);
bench("10 cookies", () => {
cookie.stringifyCookie(cookies10);
});

const cookies100 = genCookies(100);
bench("100 cookies", () => {
cookie.stringifyCookie(cookies100);
});
});

function genCookies(num: number) {
const cookies: Record<string, string | undefined> = {};

for (let i = 0; i < num; i++) {
cookies["foo" + i] = "bar" + i;
}

return cookies;
}
71 changes: 71 additions & 0 deletions src/stringify-set-cookie.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { describe, bench } from "vitest";
import * as cookie from "./index.js";

describe("cookie.stringifySetCookie", () => {
bench("simple", () => {
cookie.stringifySetCookie("foo", "bar");
});

bench("encode", () => {
cookie.stringifySetCookie("foo", "hello there!");
});

const expires = new Date("Wed, 21 Oct 2015 07:28:00 GMT");
bench("attributes", () => {
cookie.stringifySetCookie("foo", "bar", {
path: "/",
domain: "example.com",
maxAge: 3600,
expires,
httpOnly: true,
secure: true,
partitioned: true,
priority: "high",
sameSite: "lax",
});
});

bench("object input", () => {
cookie.stringifySetCookie({
name: "foo",
value: "bar",
path: "/",
maxAge: 3600,
httpOnly: true,
secure: true,
sameSite: "strict",
});
});

const setCookies10 = genSetCookies(10);
bench("10 set-cookies", () => {
for (const setCookie of setCookies10) {
cookie.stringifySetCookie(setCookie);
}
});

const setCookies100 = genSetCookies(100);
bench("100 set-cookies", () => {
for (const setCookie of setCookies100) {
cookie.stringifySetCookie(setCookie);
}
});
});

function genSetCookies(num: number): cookie.SetCookie[] {
const cookies: cookie.SetCookie[] = [];

for (let i = 0; i < num; i++) {
cookies.push({
name: "foo" + i,
value: "bar " + i,
path: "/foo" + i,
maxAge: 3600,
httpOnly: true,
secure: true,
sameSite: "lax",
});
}

return cookies;
}
Loading