Skip to content

Commit 16b8946

Browse files
committed
Small perf optimization for string getters
1 parent fdfa62e commit 16b8946

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

packages/headers/src/lib/super-headers.ts

+29-24
Original file line numberDiff line numberDiff line change
@@ -382,11 +382,11 @@ export class SuperHeaders extends Headers {
382382
* [HTTP/1.1 Specification](https://datatracker.ietf.org/doc/html/rfc7233#section-2.3)
383383
*/
384384
get acceptRanges(): string | null {
385-
return this.get('accept-ranges');
385+
return this.#getStringValue('accept-ranges');
386386
}
387387

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

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

434434
set connection(value: string | undefined | null) {
435-
this.#setValue('connection', value);
435+
this.#setStringValue('connection', value);
436436
}
437437

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

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

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

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

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

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

594594
set host(value: string | undefined | null) {
595-
this.#setValue('host', value);
595+
this.#setStringValue('host', value);
596596
}
597597

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

656656
set location(value: string | undefined | null) {
657-
this.#setValue('location', value);
657+
this.#setStringValue('location', value);
658658
}
659659

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

672672
set referer(value: string | undefined | null) {
673-
this.#setValue('referer', value);
673+
this.#setStringValue('referer', value);
674674
}
675675

676676
/**
@@ -707,14 +707,6 @@ export class SuperHeaders extends Headers {
707707

708708
// helpers
709709

710-
#setValue(key: string, value: string | undefined | null): void {
711-
if (value != null) {
712-
this.#map.set(key, value);
713-
} else {
714-
this.#map.delete(key);
715-
}
716-
}
717-
718710
#getHeaderValue<T extends HeaderValue>(key: string, ctor: new (init?: any) => T): T {
719711
let value = this.#map.get(key);
720712

@@ -771,4 +763,17 @@ export class SuperHeaders extends Headers {
771763
this.#map.delete(key);
772764
}
773765
}
766+
767+
#getStringValue(key: string): string | null {
768+
let value = this.#map.get(key);
769+
return value === undefined ? null : (value as string);
770+
}
771+
772+
#setStringValue(key: string, value: string | undefined | null): void {
773+
if (value != null) {
774+
this.#map.set(key, value);
775+
} else {
776+
this.#map.delete(key);
777+
}
778+
}
774779
}

0 commit comments

Comments
 (0)