Skip to content

Commit 4cc10d5

Browse files
trivikrBethGriggs
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 cd700ff commit 4cc10d5

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
@@ -141,9 +141,8 @@ function maybeEnableKeylog(eventName) {
141141
agent.emit('keylog', keylog, this);
142142
};
143143
// Existing sockets will start listening on keylog now.
144-
const sockets = ObjectValues(this.sockets);
145-
for (let i = 0; i < sockets.length; i++) {
146-
sockets[i].on('keylog', this[kOnKeylog]);
144+
for (const socket of ObjectValues(this.sockets)) {
145+
socket.on('keylog', this[kOnKeylog]);
147146
}
148147
}
149148
}
@@ -336,9 +335,7 @@ Agent.prototype.removeSocket = function removeSocket(s, options) {
336335
if (!s.writable)
337336
sets.push(this.freeSockets);
338337

339-
for (let sk = 0; sk < sets.length; sk++) {
340-
const sockets = sets[sk];
341-
338+
for (const sockets of sets) {
342339
if (sockets[name]) {
343340
const index = sockets[name].indexOf(s);
344341
if (index !== -1) {
@@ -373,14 +370,10 @@ Agent.prototype.reuseSocket = function reuseSocket(socket, req) {
373370
};
374371

375372
Agent.prototype.destroy = function destroy() {
376-
const sets = [this.freeSockets, this.sockets];
377-
for (let s = 0; s < sets.length; s++) {
378-
const set = sets[s];
379-
const keys = ObjectKeys(set);
380-
for (let v = 0; v < keys.length; v++) {
381-
const setName = set[keys[v]];
382-
for (let n = 0; n < setName.length; n++) {
383-
setName[n].destroy();
373+
for (const set of [this.freeSockets, this.sockets]) {
374+
for (const key of ObjectKeys(set)) {
375+
for (const setName of set[key]) {
376+
setName.destroy();
384377
}
385378
}
386379
}

lib/_http_client.js

+2
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ function ClientRequest(input, options, cb) {
234234
if (!headersArray) {
235235
if (options.headers) {
236236
const keys = ObjectKeys(options.headers);
237+
// Retain for(;;) loop for performance reasons
238+
// Refs: https://github.com/nodejs/node/pull/30958
237239
for (let i = 0; i < keys.length; i++) {
238240
const key = keys[i];
239241
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]];
@@ -180,6 +182,8 @@ ObjectDefineProperty(OutgoingMessage.prototype, '_headerNames', {
180182
if (headers !== null) {
181183
const out = ObjectCreate(null);
182184
const keys = ObjectKeys(headers);
185+
// Retain for(;;) loop for performance reasons
186+
// Refs: https://github.com/nodejs/node/pull/30958
183187
for (var i = 0; i < keys.length; ++i) {
184188
const key = keys[i];
185189
const val = headers[key][0];
@@ -195,6 +199,8 @@ ObjectDefineProperty(OutgoingMessage.prototype, '_headerNames', {
195199
if (!headers)
196200
return;
197201
const keys = ObjectKeys(val);
202+
// Retain for(;;) loop for performance reasons
203+
// Refs: https://github.com/nodejs/node/pull/30958
198204
for (var i = 0; i < keys.length; ++i) {
199205
const header = headers[keys[i]];
200206
if (header)
@@ -215,6 +221,8 @@ OutgoingMessage.prototype._renderHeaders = function _renderHeaders() {
215221

216222
if (headersMap !== null) {
217223
const keys = ObjectKeys(headersMap);
224+
// Retain for(;;) loop for performance reasons
225+
// Refs: https://github.com/nodejs/node/pull/30958
218226
for (var i = 0, l = keys.length; i < l; i++) {
219227
const key = keys[i];
220228
headers[headersMap[key][0]] = headersMap[key][1];
@@ -449,6 +457,8 @@ function processHeader(self, state, key, value, validate) {
449457
validateHeaderName(key);
450458
if (ArrayIsArray(value)) {
451459
if (value.length < 2 || !isCookieField(key)) {
460+
// Retain for(;;) loop for performance reasons
461+
// Refs: https://github.com/nodejs/node/pull/30958
452462
for (var i = 0; i < value.length; i++)
453463
storeHeader(self, state, key, value[i], validate);
454464
return;
@@ -550,6 +560,8 @@ OutgoingMessage.prototype.getHeaders = function getHeaders() {
550560
const ret = ObjectCreate(null);
551561
if (headers) {
552562
const keys = ObjectKeys(headers);
563+
// Retain for(;;) loop for performance reasons
564+
// Refs: https://github.com/nodejs/node/pull/30958
553565
for (var i = 0; i < keys.length; ++i) {
554566
const key = keys[i];
555567
const val = headers[key][1];
@@ -691,6 +703,8 @@ OutgoingMessage.prototype.addTrailers = function addTrailers(headers) {
691703
const keys = ObjectKeys(headers);
692704
const isArray = ArrayIsArray(headers);
693705
var field, value;
706+
// Retain for(;;) loop for performance reasons
707+
// Refs: https://github.com/nodejs/node/pull/30958
694708
for (var i = 0, l = keys.length; i < l; i++) {
695709
var key = keys[i];
696710
if (isArray) {
@@ -838,6 +852,8 @@ OutgoingMessage.prototype._flushOutput = function _flushOutput(socket) {
838852
const outputData = this.outputData;
839853
socket.cork();
840854
let ret;
855+
// Retain for(;;) loop for performance reasons
856+
// Refs: https://github.com/nodejs/node/pull/30958
841857
for (var i = 0; i < outputLength; i++) {
842858
const { data, encoding, callback } = outputData[i];
843859
ret = socket.write(data, encoding, callback);

lib/_http_server.js

+2
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ function writeHead(statusCode, reason, obj) {
266266
let k;
267267
if (obj) {
268268
const keys = ObjectKeys(obj);
269+
// Retain for(;;) loop for performance reasons
270+
// Refs: https://github.com/nodejs/node/pull/30958
269271
for (let i = 0; i < keys.length; i++) {
270272
k = keys[i];
271273
if (k) this.setHeader(k, obj[k]);

0 commit comments

Comments
 (0)