Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: use for...of #30958

Closed
wants to merge 10 commits into from
21 changes: 7 additions & 14 deletions lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,8 @@ function maybeEnableKeylog(eventName) {
agent.emit('keylog', keylog, this);
};
// Existing sockets will start listening on keylog now.
const sockets = ObjectValues(this.sockets);
for (let i = 0; i < sockets.length; i++) {
sockets[i].on('keylog', this[kOnKeylog]);
for (const socket of ObjectValues(this.sockets)) {
socket.on('keylog', this[kOnKeylog]);
}
}
}
Expand Down Expand Up @@ -346,9 +345,7 @@ Agent.prototype.removeSocket = function removeSocket(s, options) {
if (!s.writable)
sets.push(this.freeSockets);

for (let sk = 0; sk < sets.length; sk++) {
const sockets = sets[sk];

for (const sockets of sets) {
if (sockets[name]) {
const index = sockets[name].indexOf(s);
if (index !== -1) {
Expand Down Expand Up @@ -383,14 +380,10 @@ Agent.prototype.reuseSocket = function reuseSocket(socket, req) {
};

Agent.prototype.destroy = function destroy() {
const sets = [this.freeSockets, this.sockets];
for (let s = 0; s < sets.length; s++) {
const set = sets[s];
const keys = ObjectKeys(set);
for (let v = 0; v < keys.length; v++) {
const setName = set[keys[v]];
for (let n = 0; n < setName.length; n++) {
setName[n].destroy();
for (const set of [this.freeSockets, this.sockets]) {
for (const key of ObjectKeys(set)) {
for (const setName of set[key]) {
setName.destroy();
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ function ClientRequest(input, options, cb) {
if (!headersArray) {
if (options.headers) {
const keys = ObjectKeys(options.headers);
// Retain for(;;) loop for performance reasons
// Refs: https://github.com/nodejs/node/pull/30958
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
this.setHeader(key, options.headers[key]);
Expand Down
16 changes: 16 additions & 0 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ ObjectDefineProperty(OutgoingMessage.prototype, '_headers', {
} else if (typeof val === 'object') {
const headers = this[kOutHeaders] = ObjectCreate(null);
const keys = ObjectKeys(val);
// Retain for(;;) loop for performance reasons
// Refs: https://github.com/nodejs/node/pull/30958
for (var i = 0; i < keys.length; ++i) {
const name = keys[i];
headers[name.toLowerCase()] = [name, val[name]];
Expand All @@ -189,6 +191,8 @@ ObjectDefineProperty(OutgoingMessage.prototype, '_headerNames', {
if (headers !== null) {
const out = ObjectCreate(null);
const keys = ObjectKeys(headers);
// Retain for(;;) loop for performance reasons
// Refs: https://github.com/nodejs/node/pull/30958
for (var i = 0; i < keys.length; ++i) {
const key = keys[i];
const val = headers[key][0];
Expand All @@ -204,6 +208,8 @@ ObjectDefineProperty(OutgoingMessage.prototype, '_headerNames', {
if (!headers)
return;
const keys = ObjectKeys(val);
// Retain for(;;) loop for performance reasons
// Refs: https://github.com/nodejs/node/pull/30958
for (var i = 0; i < keys.length; ++i) {
const header = headers[keys[i]];
if (header)
Expand All @@ -224,6 +230,8 @@ OutgoingMessage.prototype._renderHeaders = function _renderHeaders() {

if (headersMap !== null) {
const keys = ObjectKeys(headersMap);
// Retain for(;;) loop for performance reasons
// Refs: https://github.com/nodejs/node/pull/30958
for (var i = 0, l = keys.length; i < l; i++) {
const key = keys[i];
headers[headersMap[key][0]] = headersMap[key][1];
Expand Down Expand Up @@ -458,6 +466,8 @@ function processHeader(self, state, key, value, validate) {
validateHeaderName(key);
if (ArrayIsArray(value)) {
if (value.length < 2 || !isCookieField(key)) {
// Retain for(;;) loop for performance reasons
// Refs: https://github.com/nodejs/node/pull/30958
for (var i = 0; i < value.length; i++)
storeHeader(self, state, key, value[i], validate);
return;
Expand Down Expand Up @@ -559,6 +569,8 @@ OutgoingMessage.prototype.getHeaders = function getHeaders() {
const ret = ObjectCreate(null);
if (headers) {
const keys = ObjectKeys(headers);
// Retain for(;;) loop for performance reasons
// Refs: https://github.com/nodejs/node/pull/30958
for (var i = 0; i < keys.length; ++i) {
const key = keys[i];
const val = headers[key][1];
Expand Down Expand Up @@ -700,6 +712,8 @@ OutgoingMessage.prototype.addTrailers = function addTrailers(headers) {
const keys = ObjectKeys(headers);
const isArray = ArrayIsArray(headers);
var field, value;
// Retain for(;;) loop for performance reasons
// Refs: https://github.com/nodejs/node/pull/30958
for (var i = 0, l = keys.length; i < l; i++) {
var key = keys[i];
if (isArray) {
Expand Down Expand Up @@ -854,6 +868,8 @@ OutgoingMessage.prototype._flushOutput = function _flushOutput(socket) {
const outputData = this.outputData;
socket.cork();
let ret;
// Retain for(;;) loop for performance reasons
// Refs: https://github.com/nodejs/node/pull/30958
for (var i = 0; i < outputLength; i++) {
const { data, encoding, callback } = outputData[i];
trivikr marked this conversation as resolved.
Show resolved Hide resolved
ret = socket.write(data, encoding, callback);
Expand Down
2 changes: 2 additions & 0 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ function writeHead(statusCode, reason, obj) {
let k;
if (obj) {
const keys = ObjectKeys(obj);
// Retain for(;;) loop for performance reasons
// Refs: https://github.com/nodejs/node/pull/30958
for (let i = 0; i < keys.length; i++) {
k = keys[i];
if (k) this.setHeader(k, obj[k]);
Expand Down