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

remove hasOwnProperty call #183

Merged
merged 4 commits into from
Oct 8, 2024
Merged
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: 5 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ const domainValueRegExp =
const pathValueRegExp = /^[\u0020-\u003A\u003D-\u007E]*$/;

const __toString = Object.prototype.toString;
const __hasOwnProperty = Object.prototype.hasOwnProperty;

const NullObject = /* @__PURE__ */ (() => {
const C = function () {};
Expand Down Expand Up @@ -94,8 +93,8 @@ export interface ParseOptions {
export function parse(
str: string,
options?: ParseOptions,
): Record<string, string> {
const obj: Record<string, string> = new NullObject();
): Record<string, string | undefined> {
const obj: Record<string, string | undefined> = new NullObject();
const len = str.length;
// RFC 6265 sec 4.1.1, RFC 2616 2.2 defines a cookie name consists of one char minimum, plus '='.
if (len < 2) return obj;
Expand All @@ -121,7 +120,7 @@ export function parse(
const key = str.slice(keyStartIdx, keyEndIdx);

// only assign once
if (!__hasOwnProperty.call(obj, key)) {
if (obj[key] === undefined) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you double check performance? I didn’t change this because my numbers were worse doing the undefined check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, let me bench it

Copy link
Member

Choose a reason for hiding this comment

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

Bench LGTM, would you be free to rebase on main and remove the if (value !== undefined) now? Since it won't matter if dec returns undefined from #179.

let valStartIdx = startIndex(str, eqIdx + 1, endIdx);
let valEndIdx = endIndex(str, endIdx, valStartIdx);

Expand All @@ -134,7 +133,7 @@ export function parse(
}

const value = dec(str.slice(valStartIdx, valEndIdx));
if (value !== undefined) obj[key] = value;
obj[key] = value;
}

index = endIdx + 1;
Expand Down Expand Up @@ -366,7 +365,7 @@ export function serialize(
/**
* URL-decode string value. Optimized to skip native call when no %.
*/
function decode(str: string): string | undefined {
function decode(str: string): string {
if (str.indexOf("%") === -1) return str;

try {
Expand Down
Loading