Skip to content

Commit 2b867d2

Browse files
notarseniyMylesBorins
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 28471c2 commit 2b867d2

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

lib/internal/child_process.js

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,16 @@ util.inherits(ChildProcess, EventEmitter);
226226

227227

228228
function flushStdio(subprocess) {
229-
if (subprocess.stdio == null) return;
230-
subprocess.stdio.forEach(function(stream, fd, stdio) {
229+
const stdio = subprocess.stdio;
230+
231+
if (stdio == null) return;
232+
233+
for (var i = 0; i < stdio.length; i++) {
234+
const stream = stdio[i];
231235
if (!stream || !stream.readable || stream._readableState.readableListening)
232-
return;
236+
continue;
233237
stream.resume();
234-
});
238+
}
235239
}
236240

237241

@@ -264,6 +268,7 @@ ChildProcess.prototype.spawn = function(options) {
264268
const self = this;
265269
var ipc;
266270
var ipcFd;
271+
var i;
267272
// If no `stdio` option was given - use default
268273
var stdio = options.stdio || 'pipe';
269274

@@ -298,11 +303,12 @@ ChildProcess.prototype.spawn = function(options) {
298303
if (err !== uv.UV_ENOENT) return err;
299304
} else if (err) {
300305
// Close all opened fds on error
301-
stdio.forEach(function(stdio) {
302-
if (stdio.type === 'pipe') {
303-
stdio.handle.close();
306+
for (i = 0; i < stdio.length; i++) {
307+
const stream = stdio[i];
308+
if (stream.type === 'pipe') {
309+
stream.handle.close();
304310
}
305-
});
311+
}
306312

307313
this._handle.close();
308314
this._handle = null;
@@ -311,27 +317,29 @@ ChildProcess.prototype.spawn = function(options) {
311317

312318
this.pid = this._handle.pid;
313319

314-
stdio.forEach(function(stdio, i) {
315-
if (stdio.type === 'ignore') return;
320+
for (i = 0; i < stdio.length; i++) {
321+
const stream = stdio[i];
322+
if (stream.type === 'ignore') continue;
316323

317-
if (stdio.ipc) {
324+
if (stream.ipc) {
318325
self._closesNeeded++;
319-
return;
326+
continue;
320327
}
321328

322-
if (stdio.handle) {
329+
if (stream.handle) {
323330
// when i === 0 - we're dealing with stdin
324331
// (which is the only one writable pipe)
325-
stdio.socket = createSocket(self.pid !== 0 ? stdio.handle : null, i > 0);
332+
stream.socket = createSocket(self.pid !== 0 ?
333+
stream.handle : null, i > 0);
326334

327335
if (i > 0 && self.pid !== 0) {
328336
self._closesNeeded++;
329-
stdio.socket.on('close', function() {
337+
stream.socket.on('close', function() {
330338
maybeClose(self);
331339
});
332340
}
333341
}
334-
});
342+
}
335343

336344
this.stdin = stdio.length >= 1 && stdio[0].socket !== undefined ?
337345
stdio[0].socket : null;
@@ -760,11 +768,11 @@ function _validateStdio(stdio, sync) {
760768
}
761769

762770
// Defaults
763-
if (stdio === null || stdio === undefined) {
771+
if (stdio == null) {
764772
stdio = i < 3 ? 'pipe' : 'ignore';
765773
}
766774

767-
if (stdio === null || stdio === 'ignore') {
775+
if (stdio === 'ignore') {
768776
acc.push({type: 'ignore'});
769777
} else if (stdio === 'pipe' || typeof stdio === 'number' && stdio < 0) {
770778
var a = {
@@ -850,7 +858,7 @@ function getSocketList(type, slave, key) {
850858
function maybeClose(subprocess) {
851859
subprocess._closesGot++;
852860

853-
if (subprocess._closesGot == subprocess._closesNeeded) {
861+
if (subprocess._closesGot === subprocess._closesNeeded) {
854862
subprocess.emit('close', subprocess.exitCode, subprocess.signalCode);
855863
}
856864
}

0 commit comments

Comments
 (0)