Performance: process.exit and exit codes
This affects how quickly a Node.js process terminates and releases resources, impacting server responsiveness and resource cleanup.
Jump into concepts and practice - no test required
server.close(() => {
process.exit(0);
});process.exit(0);| Pattern | Async Cleanup | Resource Release | Exit Speed | Verdict |
|---|---|---|---|---|
| Immediate process.exit | No | No | Fast but unsafe | [X] Bad |
| Graceful shutdown with callbacks | Yes | Yes | Slightly slower but safe | [OK] Good |
process.exit(0) do in a Node.js program?process.exit behaviorprocess.exit immediately stops the Node.js program.00 means the program ended successfully without errors.process.exit() with a code inside parentheses.process.exit(1); is the correct syntax to exit with code 1. Other options misuse method or assignment.console.log('Start');
process.exit(2);
console.log('End');process.exit(2), which stops the program immediately.2. The line printing 'End' never runs.process.exit = 1;
console.log('Exiting');
process.exit();1 to process.exit, replacing the function with a number.process.exit() after overwriting causes an error because it's no longer a function.import fs from 'fs';
const file = 'data.txt';
if (fs.existsSync(file)) {
process.exit(0);
} else {
process.exit(3);
}fs.existsSync checks synchronously if the file exists, returning true or false.process.exit(0) runs; else process.exit(3) runs. This matches the requirement.