Performance: setTimeout and clearTimeout
MEDIUM IMPACT
This concept affects how delayed tasks impact event loop responsiveness and CPU usage in Node.js applications.
const timer = setTimeout(() => {
console.log('Task executed');
}, 10000);
// Cancel timer if task is no longer needed
clearTimeout(timer);const timer = setTimeout(() => {
console.log('Task executed');
}, 10000);
// No clearTimeout called even if task no longer needed| Pattern | Event Loop Impact | CPU Usage | Responsiveness | Verdict |
|---|---|---|---|---|
| setTimeout without clearTimeout | Schedules callback even if unused | Higher due to unnecessary callback | Lower due to event loop delay | [X] Bad |
| setTimeout with clearTimeout | Cancels unused callback | Lower CPU usage | Higher responsiveness | [OK] Good |