Skip to content

Commit 32b6bcd

Browse files
mscdexevanlucas
authored andcommitted
http: optimize headers iteration
This commit uses instanceof instead of Array.isArray() for faster type checking and avoids calling Object.keys() when the headers are stored as a 2D array instead of a plain object. PR-URL: #6533 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Fedor Indutny <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent a760d70 commit 32b6bcd

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

lib/_http_outgoing.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -209,23 +209,31 @@ function _storeHeader(firstLine, headers) {
209209
messageHeader: firstLine
210210
};
211211

212-
if (headers) {
213-
var keys = Object.keys(headers);
214-
var isArray = Array.isArray(headers);
215-
var field, value;
216-
217-
for (var i = 0, l = keys.length; i < l; i++) {
218-
var key = keys[i];
219-
if (isArray) {
220-
field = headers[key][0];
221-
value = headers[key][1];
212+
var i;
213+
var j;
214+
var field;
215+
var value;
216+
if (headers instanceof Array) {
217+
for (i = 0; i < headers.length; ++i) {
218+
field = headers[i][0];
219+
value = headers[i][1];
220+
221+
if (value instanceof Array) {
222+
for (j = 0; j < value.length; j++) {
223+
storeHeader(this, state, field, value[j]);
224+
}
222225
} else {
223-
field = key;
224-
value = headers[key];
226+
storeHeader(this, state, field, value);
225227
}
228+
}
229+
} else if (headers) {
230+
var keys = Object.keys(headers);
231+
for (i = 0; i < keys.length; ++i) {
232+
field = keys[i];
233+
value = headers[field];
226234

227-
if (Array.isArray(value)) {
228-
for (var j = 0; j < value.length; j++) {
235+
if (value instanceof Array) {
236+
for (j = 0; j < value.length; j++) {
229237
storeHeader(this, state, field, value[j]);
230238
}
231239
} else {

0 commit comments

Comments
 (0)