Skip to content

Commit c371fdc

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 <cjihrig@gmail.com> Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Brian White <mscdex@mscdex.net>
1 parent 1dddfec commit c371fdc

1 file changed

Lines changed: 27 additions & 19 deletions

File tree

‎lib/internal/child_process.js‎

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

231231

232232
functionflushStdio(subprocess){
233-
if(subprocess.stdio==null)return;
234-
subprocess.stdio.forEach(function(stream,fd,stdio){
233+
conststdio=subprocess.stdio;
234+
235+
if(stdio==null)return;
236+
237+
for(vari=0;i<stdio.length;i++){
238+
conststream=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
constself=this;
269273
varipc;
270274
varipcFd;
275+
vari;
271276
// If no `stdio` option was given - use default
272277
varstdio=options.stdio||'pipe';
273278

@@ -302,11 +307,12 @@ ChildProcess.prototype.spawn = function(options) {
302307
if(err!==uv.UV_ENOENT)returnerr;
303308
}elseif(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+
conststream=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+
conststream=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;
@@ -783,11 +791,11 @@ function _validateStdio(stdio, sync) {
783791
}
784792

785793
// Defaults
786-
if(stdio===null||stdio===undefined){
794+
if(stdio==null){
787795
stdio=i<3 ? 'pipe' : 'ignore';
788796
}
789797

790-
if(stdio===null||stdio==='ignore'){
798+
if(stdio==='ignore'){
791799
acc.push({type: 'ignore'});
792800
}elseif(stdio==='pipe'||typeofstdio==='number'&&stdio<0){
793801
vara={
@@ -873,7 +881,7 @@ function getSocketList(type, slave, key) {
873881
functionmaybeClose(subprocess){
874882
subprocess._closesGot++;
875883

876-
if(subprocess._closesGot==subprocess._closesNeeded){
884+
if(subprocess._closesGot===subprocess._closesNeeded){
877885
subprocess.emit('close',subprocess.exitCode,subprocess.signalCode);
878886
}
879887
}

0 commit comments

Comments
 (0)