Avoiding leaks — closures, timers, references
Prevent memory leaks by clearing timers, limiting closures, and dropping unused references; use tiny patterns that beginners can apply.
What is a leak?
Goal: Avoid simple leaks.
- Always clear timers
- Do not keep unused references
- Keep closures small
- Prefer short-lived data

Clear intervals
Keep the interval id; call clearInterval during cleanup so the timer and its captures can be collected.
// Start an interval and then clear it to avoid leaks
function startTicker() {
// store id so we can clear later
const id = setInterval(function () {
// work would run every X ms; omitted for demo
}, 1000);
// simulate screen close / cleanup
clearInterval(id);
console.log("interval cleared");
}
startTicker();

All lessons in this course
- Big-O basics, hot vs cold paths (beginner wins)
- Avoiding leaks — closures, timers, references
- Profiling intro (Node/DevTools) — tiny timing habits