Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Understanding Debounce and Throttle in JavaScript

Tech 1

When dealing with events that fire frequently, such as window resizing or keyboard input, executing heavy operations on every event can severely impact performance. Two common techniques to control the execution rate of functions are debouncing and throttling.

Debounce

Debouncing ensures that a function is only executed after a specified period of inactivity. If the event is triggered again before the delay period ends, the timer resets. This means the event handler will only execute once within a given time limit, specifically after the user has stopped triggering the event for the defined duration.

javascript function createDebouncedTask(callback, delayMs) { let pendingTimer = null;

return function(...params) {
    if (pendingTimer) {
        clearTimeout(pendingTimer);
    }
    
    const currentContext = this;
    
    pendingTimer = setTimeout(() => {
        callback.apply(currentContext, params);
        pendingTimer = null;
    }, delayMs);
};

}

// Usage Example: const handleDebouncedInput = createDebouncedTask(() => { console.log('Debounce successful!'); }, 500);

const debounceInputElement = document.getElementById('debounce'); debounceInputElement.addEventListener('input', handleDebouncedInput);

Throttle

Throttling ensures that a function executes at most once within a specified time period. If a event is triggered continuously in a short amount of time, the function will run once, and then become inactive for the remaining duration of the time limit. It only becomes eligible to run again once the cooling period has elapsed.

javascript function createThrottledTask(callback, intervalMs) { let isWaiting = false;

return function(...params) {
    if (!isWaiting) {
        const currentContext = this;
        
        isWaiting = true;
        setTimeout(() => {
            callback.apply(currentContext, params);
            isWaiting = false;
        }, intervalMs);
    }
};

}

// Usage Example: const handleThrottledInput = createThrottledTask(() => { console.log('Throttle successful!'); }, 500);

const throttleInputElement = document.getElementById('throttle'); throttleInputElement.addEventListener('input', handleThrottledInput);

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.