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: https://github.com/nodejs/node/pull/6533
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
This commit is contained in:
Brian White 2016-12-20 02:53:47 -05:00
parent af74e72a9f
commit 4d7531de24
No known key found for this signature in database
GPG Key ID: 606D7358F94DA209

View File

@ -209,23 +209,31 @@ function _storeHeader(firstLine, headers) {
messageHeader: firstLine messageHeader: firstLine
}; };
if (headers) { var i;
var keys = Object.keys(headers); var j;
var isArray = Array.isArray(headers); var field;
var field, value; var value;
if (headers instanceof Array) {
for (i = 0; i < headers.length; ++i) {
field = headers[i][0];
value = headers[i][1];
for (var i = 0, l = keys.length; i < l; i++) { if (value instanceof Array) {
var key = keys[i]; for (j = 0; j < value.length; j++) {
if (isArray) { storeHeader(this, state, field, value[j]);
field = headers[key][0]; }
value = headers[key][1];
} else { } else {
field = key; storeHeader(this, state, field, value);
value = headers[key];
} }
}
} else if (headers) {
var keys = Object.keys(headers);
for (i = 0; i < keys.length; ++i) {
field = keys[i];
value = headers[field];
if (Array.isArray(value)) { if (value instanceof Array) {
for (var j = 0; j < value.length; j++) { for (j = 0; j < value.length; j++) {
storeHeader(this, state, field, value[j]); storeHeader(this, state, field, value[j]);
} }
} else { } else {