Skip to content

Commit c942e20

Browse files
notarseniyaddaleax
authored andcommitted
child_process: refactor internal/child_process.js
* Prefer === to == where possible * Remove condition that will always be false * Prefer for-loop statements to forEach where possible for perfomance reasons PR-URL: #11366 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Brian White <[email protected]>
1 parent b457f38 commit c942e20

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

lib/internal/child_process.js

+27-19
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,16 @@ util.inherits(ChildProcess, EventEmitter);
230230

231231

232232
function flushStdio(subprocess) {
233-
if (subprocess.stdio == null) return;
234-
subprocess.stdio.forEach(function(stream, fd, stdio) {
233+
const stdio = subprocess.stdio;
234+
235+
if (stdio == null) return;
236+
237+
for (var i = 0; i < stdio.length; i++) {
238+
const stream = stdio[i];
235239
if (!stream || !stream.readable || stream._readableState.readableListening)
236-
return;
240+
continue;
237241
stream.resume();
238-
});
242+
}
239243
}
240244

241245

@@ -268,6 +272,7 @@ ChildProcess.prototype.spawn = function(options) {
268272
const self = this;
269273
var ipc;
270274
var ipcFd;
275+
var i;
271276
// If no `stdio` option was given - use default
272277
var stdio = options.stdio || 'pipe';
273278

@@ -302,11 +307,12 @@ ChildProcess.prototype.spawn = function(options) {
302307
if (err !== uv.UV_ENOENT) return err;
303308
} else if (err) {
304309
// Close all opened fds on error
305-
stdio.forEach(function(stdio) {
306-
if (stdio.type === 'pipe') {
307-
stdio.handle.close();
310+
for (i = 0; i < stdio.length; i++) {
311+
const stream = stdio[i];
312+
if (stream.type === 'pipe') {
313+
stream.handle.close();
308314
}
309-
});
315+
}
310316

311317
this._handle.close();
312318
this._handle = null;
@@ -315,27 +321,29 @@ ChildProcess.prototype.spawn = function(options) {
315321

316322
this.pid = this._handle.pid;
317323

318-
stdio.forEach(function(stdio, i) {
319-
if (stdio.type === 'ignore') return;
324+
for (i = 0; i < stdio.length; i++) {
325+
const stream = stdio[i];
326+
if (stream.type === 'ignore') continue;
320327

321-
if (stdio.ipc) {
328+
if (stream.ipc) {
322329
self._closesNeeded++;
323-
return;
330+
continue;
324331
}
325332

326-
if (stdio.handle) {
333+
if (stream.handle) {
327334
// when i === 0 - we're dealing with stdin
328335
// (which is the only one writable pipe)
329-
stdio.socket = createSocket(self.pid !== 0 ? stdio.handle : null, i > 0);
336+
stream.socket = createSocket(self.pid !== 0 ?
337+
stream.handle : null, i > 0);
330338

331339
if (i > 0 && self.pid !== 0) {
332340
self._closesNeeded++;
333-
stdio.socket.on('close', function() {
341+
stream.socket.on('close', function() {
334342
maybeClose(self);
335343
});
336344
}
337345
}
338-
});
346+
}
339347

340348
this.stdin = stdio.length >= 1 && stdio[0].socket !== undefined ?
341349
stdio[0].socket : null;
@@ -797,11 +805,11 @@ function _validateStdio(stdio, sync) {
797805
}
798806

799807
// Defaults
800-
if (stdio === null || stdio === undefined) {
808+
if (stdio == null) {
801809
stdio = i < 3 ? 'pipe' : 'ignore';
802810
}
803811

804-
if (stdio === null || stdio === 'ignore') {
812+
if (stdio === 'ignore') {
805813
acc.push({type: 'ignore'});
806814
} else if (stdio === 'pipe' || typeof stdio === 'number' && stdio < 0) {
807815
var a = {
@@ -887,7 +895,7 @@ function getSocketList(type, slave, key) {
887895
function maybeClose(subprocess) {
888896
subprocess._closesGot++;
889897

890-
if (subprocess._closesGot == subprocess._closesNeeded) {
898+
if (subprocess._closesGot === subprocess._closesNeeded) {
891899
subprocess.emit('close', subprocess.exitCode, subprocess.signalCode);
892900
}
893901
}

0 commit comments

Comments
 (0)