Skip to content

Commit 3bc9b09

Browse files
trivikrMylesBorins
authored andcommitted
http: use for...of in http library code
PR-URL: #30958 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 956dec8 commit 3bc9b09

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

lib/_http_agent.js

+7-14
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,8 @@ function maybeEnableKeylog(eventName) {
146146
agent.emit('keylog', keylog, this);
147147
};
148148
// Existing sockets will start listening on keylog now.
149-
const sockets = ObjectValues(this.sockets);
150-
for (let i = 0; i < sockets.length; i++) {
151-
sockets[i].on('keylog', this[kOnKeylog]);
149+
for (const socket of ObjectValues(this.sockets)) {
150+
socket.on('keylog', this[kOnKeylog]);
152151
}
153152
}
154153
}
@@ -346,9 +345,7 @@ Agent.prototype.removeSocket = function removeSocket(s, options) {
346345
if (!s.writable)
347346
sets.push(this.freeSockets);
348347

349-
for (let sk = 0; sk < sets.length; sk++) {
350-
const sockets = sets[sk];
351-
348+
for (const sockets of sets) {
352349
if (sockets[name]) {
353350
const index = sockets[name].indexOf(s);
354351
if (index !== -1) {
@@ -383,14 +380,10 @@ Agent.prototype.reuseSocket = function reuseSocket(socket, req) {
383380
};
384381

385382
Agent.prototype.destroy = function destroy() {
386-
const sets = [this.freeSockets, this.sockets];
387-
for (let s = 0; s < sets.length; s++) {
388-
const set = sets[s];
389-
const keys = ObjectKeys(set);
390-
for (let v = 0; v < keys.length; v++) {
391-
const setName = set[keys[v]];
392-
for (let n = 0; n < setName.length; n++) {
393-
setName[n].destroy();
383+
for (const set of [this.freeSockets, this.sockets]) {
384+
for (const key of ObjectKeys(set)) {
385+
for (const setName of set[key]) {
386+
setName.destroy();
394387
}
395388
}
396389
}

lib/_http_client.js

+2
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ function ClientRequest(input, options, cb) {
231231
if (!headersArray) {
232232
if (options.headers) {
233233
const keys = ObjectKeys(options.headers);
234+
// Retain for(;;) loop for performance reasons
235+
// Refs: https://github.com/nodejs/node/pull/30958
234236
for (let i = 0; i < keys.length; i++) {
235237
const key = keys[i];
236238
this.setHeader(key, options.headers[key]);

lib/_http_outgoing.js

+16
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ ObjectDefineProperty(OutgoingMessage.prototype, '_headers', {
166166
} else if (typeof val === 'object') {
167167
const headers = this[kOutHeaders] = ObjectCreate(null);
168168
const keys = ObjectKeys(val);
169+
// Retain for(;;) loop for performance reasons
170+
// Refs: https://github.com/nodejs/node/pull/30958
169171
for (var i = 0; i < keys.length; ++i) {
170172
const name = keys[i];
171173
headers[name.toLowerCase()] = [name, val[name]];
@@ -189,6 +191,8 @@ ObjectDefineProperty(OutgoingMessage.prototype, '_headerNames', {
189191
if (headers !== null) {
190192
const out = ObjectCreate(null);
191193
const keys = ObjectKeys(headers);
194+
// Retain for(;;) loop for performance reasons
195+
// Refs: https://github.com/nodejs/node/pull/30958
192196
for (var i = 0; i < keys.length; ++i) {
193197
const key = keys[i];
194198
const val = headers[key][0];
@@ -204,6 +208,8 @@ ObjectDefineProperty(OutgoingMessage.prototype, '_headerNames', {
204208
if (!headers)
205209
return;
206210
const keys = ObjectKeys(val);
211+
// Retain for(;;) loop for performance reasons
212+
// Refs: https://github.com/nodejs/node/pull/30958
207213
for (var i = 0; i < keys.length; ++i) {
208214
const header = headers[keys[i]];
209215
if (header)
@@ -224,6 +230,8 @@ OutgoingMessage.prototype._renderHeaders = function _renderHeaders() {
224230

225231
if (headersMap !== null) {
226232
const keys = ObjectKeys(headersMap);
233+
// Retain for(;;) loop for performance reasons
234+
// Refs: https://github.com/nodejs/node/pull/30958
227235
for (var i = 0, l = keys.length; i < l; i++) {
228236
const key = keys[i];
229237
headers[headersMap[key][0]] = headersMap[key][1];
@@ -458,6 +466,8 @@ function processHeader(self, state, key, value, validate) {
458466
validateHeaderName(key);
459467
if (ArrayIsArray(value)) {
460468
if (value.length < 2 || !isCookieField(key)) {
469+
// Retain for(;;) loop for performance reasons
470+
// Refs: https://github.com/nodejs/node/pull/30958
461471
for (var i = 0; i < value.length; i++)
462472
storeHeader(self, state, key, value[i], validate);
463473
return;
@@ -559,6 +569,8 @@ OutgoingMessage.prototype.getHeaders = function getHeaders() {
559569
const ret = ObjectCreate(null);
560570
if (headers) {
561571
const keys = ObjectKeys(headers);
572+
// Retain for(;;) loop for performance reasons
573+
// Refs: https://github.com/nodejs/node/pull/30958
562574
for (var i = 0; i < keys.length; ++i) {
563575
const key = keys[i];
564576
const val = headers[key][1];
@@ -700,6 +712,8 @@ OutgoingMessage.prototype.addTrailers = function addTrailers(headers) {
700712
const keys = ObjectKeys(headers);
701713
const isArray = ArrayIsArray(headers);
702714
var field, value;
715+
// Retain for(;;) loop for performance reasons
716+
// Refs: https://github.com/nodejs/node/pull/30958
703717
for (var i = 0, l = keys.length; i < l; i++) {
704718
var key = keys[i];
705719
if (isArray) {
@@ -854,6 +868,8 @@ OutgoingMessage.prototype._flushOutput = function _flushOutput(socket) {
854868
const outputData = this.outputData;
855869
socket.cork();
856870
let ret;
871+
// Retain for(;;) loop for performance reasons
872+
// Refs: https://github.com/nodejs/node/pull/30958
857873
for (var i = 0; i < outputLength; i++) {
858874
const { data, encoding, callback } = outputData[i];
859875
ret = socket.write(data, encoding, callback);

lib/_http_server.js

+2
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ function writeHead(statusCode, reason, obj) {
263263
let k;
264264
if (obj) {
265265
const keys = ObjectKeys(obj);
266+
// Retain for(;;) loop for performance reasons
267+
// Refs: https://github.com/nodejs/node/pull/30958
266268
for (let i = 0; i < keys.length; i++) {
267269
k = keys[i];
268270
if (k) this.setHeader(k, obj[k]);

0 commit comments

Comments
 (0)