Writing functioning code is only half the battle. In modern web engineering, writing high-performance, memory-efficient JavaScript that loads in milliseconds and runs smoothly at 60 FPS without memory leaks is what separates senior developers from novices.
Debouncing ensures a function runs only after a user has stopped performing an action (e.g., waiting 400ms after the user finishes typing in a search bar before dispatching an API request):
function debounce(func, delayMs) {
let timeoutId;
return function(...args) {
clearTimeout(timeoutId); // Reset countdown on every keystroke
timeoutId = setTimeout(() => {
func.apply(this, args);
}, delayMs);
};
}
// Practical usage on Search Input:
const handleLiveSearch = debounce((query) => {
console.log(`Dispatching API query: "${query}"`);
}, 400);
document.getElementById("search-input").addEventListener("input", (e) => {
handleLiveSearch(e.target.value);
});
Throttling ensures that a high-frequency event (like window scrolling) executes at most once every 200 milliseconds:
function throttle(func, intervalMs) {
let lastExecutedTime = 0;
return function(...args) {
const now = Date.now();
if (now - lastExecutedTime >= intervalMs) {
lastExecutedTime = now;
func.apply(this, args);
}
};
}
// Throttle scroll listener to once every 200ms
window.addEventListener("scroll", throttle(() => {
console.log("Scroll position evaluated at:", window.scrollY);
}, 200));
setInterval() without saving the timer ID and invoking clearInterval() when components unmount.let/const in non-strict mode creates permanent variables on the global window object that can never be garbage-collected.Exam Question: "How does the V8 Garbage Collector identify reclaimable memory? Explain Mark-and-Sweep."
Mark-and-Sweep Algorithm:
1. Roots: The GC starts at global roots (window, global variables, active execution context stack).
2. Mark: It traverses all references outward from roots and 'marks' every reachable object.
3. Sweep: Any memory allocation in the Heap that was NOT marked as reachable is swept and reclaimed.
Congratulations on mastering Tier 6: Advance JavaScript & Internals! Now let us review interview preparation, viva questions, and practice sessions. Proceed to Tier 7: JavaScript 100+ Interview Questions & Viva Q&A →
Accelerate your JavaScript mastery by studying these tightly connected core tutorials:
Preventing long tasks from freezing the UI.
Profiling CPU flame charts and memory leaks.
Optimizing network loads with client storage.