Skip to content

Commit

Permalink
Small perf optimization for string getters
Browse files Browse the repository at this point in the history
  • Loading branch information
mjackson committed Dec 20, 2024
1 parent fdfa62e commit 16b8946
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions packages/headers/src/lib/super-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,11 @@ export class SuperHeaders extends Headers {
* [HTTP/1.1 Specification](https://datatracker.ietf.org/doc/html/rfc7233#section-2.3)
*/
get acceptRanges(): string | null {
return this.get('accept-ranges');
return this.#getStringValue('accept-ranges');
}

set acceptRanges(value: string | undefined | null) {
this.#setValue('accept-ranges', value);
this.#setStringValue('accept-ranges', value);
}

/**
Expand Down Expand Up @@ -428,11 +428,11 @@ export class SuperHeaders extends Headers {
* [HTTP/1.1 Specification](https://datatracker.ietf.org/doc/html/rfc7230#section-6.1)
*/
get connection(): string | null {
return this.get('connection');
return this.#getStringValue('connection');
}

set connection(value: string | undefined | null) {
this.#setValue('connection', value);
this.#setStringValue('connection', value);
}

/**
Expand Down Expand Up @@ -461,11 +461,11 @@ export class SuperHeaders extends Headers {
* [HTTP/1.1 Specification](https://httpwg.org/specs/rfc9110.html#field.content-encoding)
*/
get contentEncoding(): string | null {
return this.get('content-encoding');
return this.#getStringValue('content-encoding');
}

set contentEncoding(value: string | string[] | undefined | null) {
this.#setValue('content-encoding', Array.isArray(value) ? value.join(', ') : value);
this.#setStringValue('content-encoding', Array.isArray(value) ? value.join(', ') : value);
}

/**
Expand All @@ -479,11 +479,11 @@ export class SuperHeaders extends Headers {
* [HTTP/1.1 Specification](https://httpwg.org/specs/rfc9110.html#field.content-language)
*/
get contentLanguage(): string | null {
return this.get('content-language');
return this.#getStringValue('content-language');
}

set contentLanguage(value: string | string[] | undefined | null) {
this.#setValue('content-language', Array.isArray(value) ? value.join(', ') : value);
this.#setStringValue('content-language', Array.isArray(value) ? value.join(', ') : value);
}

/**
Expand Down Expand Up @@ -555,11 +555,11 @@ export class SuperHeaders extends Headers {
* [HTTP/1.1 Specification](https://datatracker.ietf.org/doc/html/rfc7232#section-2.3)
*/
get etag(): string | null {
return this.get('etag');
return this.#getStringValue('etag');
}

set etag(value: string | undefined | null) {
this.#setValue(
this.#setStringValue(
'etag',
typeof value === 'string' && !/^(W\/)?".*"$/.test(value) ? `"${value}"` : value,
);
Expand Down Expand Up @@ -588,11 +588,11 @@ export class SuperHeaders extends Headers {
* [HTTP/1.1 Specification](https://datatracker.ietf.org/doc/html/rfc7230#section-5.4)
*/
get host(): string | null {
return this.get('host');
return this.#getStringValue('host');
}

set host(value: string | undefined | null) {
this.#setValue('host', value);
this.#setStringValue('host', value);
}

/**
Expand Down Expand Up @@ -650,11 +650,11 @@ export class SuperHeaders extends Headers {
* [HTTP/1.1 Specification](https://datatracker.ietf.org/doc/html/rfc7231#section-7.1.2)
*/
get location(): string | null {
return this.get('location');
return this.#getStringValue('location');
}

set location(value: string | undefined | null) {
this.#setValue('location', value);
this.#setStringValue('location', value);
}

/**
Expand All @@ -666,11 +666,11 @@ export class SuperHeaders extends Headers {
* [HTTP/1.1 Specification](https://datatracker.ietf.org/doc/html/rfc7231#section-5.5.2)
*/
get referer(): string | null {
return this.get('referer');
return this.#getStringValue('referer');
}

set referer(value: string | undefined | null) {
this.#setValue('referer', value);
this.#setStringValue('referer', value);
}

/**
Expand Down Expand Up @@ -707,14 +707,6 @@ export class SuperHeaders extends Headers {

// helpers

#setValue(key: string, value: string | undefined | null): void {
if (value != null) {
this.#map.set(key, value);
} else {
this.#map.delete(key);
}
}

#getHeaderValue<T extends HeaderValue>(key: string, ctor: new (init?: any) => T): T {
let value = this.#map.get(key);

Expand Down Expand Up @@ -771,4 +763,17 @@ export class SuperHeaders extends Headers {
this.#map.delete(key);
}
}

#getStringValue(key: string): string | null {
let value = this.#map.get(key);
return value === undefined ? null : (value as string);
}

#setStringValue(key: string, value: string | undefined | null): void {
if (value != null) {
this.#map.set(key, value);
} else {
this.#map.delete(key);
}
}
}

0 comments on commit 16b8946

Please sign in to comment.