Skip to content

Commit

Permalink
http: use for...of in http library code
Browse files Browse the repository at this point in the history
PR-URL: #30958
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
trivikr authored and BethGriggs committed Feb 6, 2020
1 parent cd700ff commit 4cc10d5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
21 changes: 7 additions & 14 deletions lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,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 @@ -336,9 +335,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 @@ -373,14 +370,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 @@ -234,6 +234,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 @@ -180,6 +182,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 @@ -195,6 +199,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 @@ -215,6 +221,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 @@ -449,6 +457,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 @@ -550,6 +560,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 @@ -691,6 +703,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 @@ -838,6 +852,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];
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 @@ -266,6 +266,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

0 comments on commit 4cc10d5

Please sign in to comment.