Skip to content

Commit e15226a

Browse files
RafaelJCamarakirjs
authored andcommitted
refactor: initialize headers map directly in HttpHeaders class (#59268)
Improved the initialization of the headers map to enhance performance and code readability. No breaking changes. PR Close #59268
1 parent d038b3c commit e15226a

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

packages/common/http/src/headers.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ export class HttpHeaders {
2323
/**
2424
* Internal map of lowercase header names to values.
2525
*/
26-
// TODO(issue/24571): remove '!'.
27-
private headers!: Map<string, string[]>;
26+
private headers: Map<string, string[]> = new Map();
2827

2928
/**
3029
* Internal map of lowercased header names to the normalized
@@ -47,11 +46,9 @@ export class HttpHeaders {
4746
constructor(
4847
headers?: string | {[name: string]: string | number | (string | number)[]} | Headers,
4948
) {
50-
if (!headers) {
51-
this.headers = new Map<string, string[]>();
52-
} else if (typeof headers === 'string') {
49+
if (!headers) return;
50+
if (typeof headers === 'string') {
5351
this.lazyInit = () => {
54-
this.headers = new Map<string, string[]>();
5552
headers.split('\n').forEach((line) => {
5653
const index = line.indexOf(':');
5754
if (index > 0) {
@@ -62,7 +59,6 @@ export class HttpHeaders {
6259
});
6360
};
6461
} else if (typeof Headers !== 'undefined' && headers instanceof Headers) {
65-
this.headers = new Map<string, string[]>();
6662
headers.forEach((value: string, name: string) => {
6763
this.addHeaderEntry(name, value);
6864
});
@@ -71,7 +67,6 @@ export class HttpHeaders {
7167
if (typeof ngDevMode === 'undefined' || ngDevMode) {
7268
assertValidHeaders(headers);
7369
}
74-
this.headers = new Map<string, string[]>();
7570
Object.entries(headers).forEach(([name, values]) => {
7671
this.setHeaderEntries(name, values);
7772
});

0 commit comments

Comments
 (0)