Performance: exec for running shell commands
HIGH IMPACT
Running shell commands with exec can cause high memory usage from buffering the entire output, potentially leading to buffer exceeded errors or GC pauses.
const { spawn } = require('child_process');
const child = spawn('some-long-running-command');
child.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
child.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
child.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});const { exec } = require('child_process');
exec('some-long-running-command', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
});| Pattern | Buffering | Memory Usage | Output Handling | Verdict |
|---|---|---|---|---|
| exec | Buffers entire output until complete | High for large outputs due to buffering | Buffers entire output before callback | [X] Bad |
| spawn | Streams output incrementally | Low, streams data incrementally | Streams output data as it arrives | [OK] Good |