Home >Web Front-end >JS Tutorial >Enhancing Performance in JavaScript: Understanding Debouncing and Throttling
Performance optimization is critical in modern web applications, especially those that involve user interactions like typing in a search bar, scrolling, or resizing a window. These actions can fire off many function calls in a short time, which can degrade performance.
To mitigate this, two common techniques are debouncing and throttling, which allow you to control the rate at which a function is invoked, leading to a smoother, more efficient experience.
Debouncing delays the execution of a function until a specified time has passed since the last event trigger. It is particularly helpful when dealing with events like search inputs, where you want to avoid making an API request on every keystroke.
Imagine a search input where you want to wait until the user has stopped typing for 300ms before making an API request. Debouncing allows you to ensure that the function is only executed after the user has paused typing, preventing unnecessary API calls.
function debounce(func, delay) { let timeout; return function () { const context = this; const args = arguments; clearTimeout(timeout); timeout = setTimeout(() => func.apply(context, args), delay); }; } function searchAPI() { console.log("API request made"); } const debouncedSearch = debounce(searchAPI, 300); debouncedSearch(); // Only triggers 300ms after the last call
Here, the API request will only be made if the user pauses for 300ms.
In contrast to debouncing, throttling ensures that a function is called at most once every specified interval, even if the event continues to trigger. This technique is ideal for scenarios like window resizing or scrolling, where the events fire continuously.
Throttling allows a function to execute only once during a defined period (e.g., 200ms), ensuring that the function is not overwhelmed by repeated triggers.
function throttle(func, limit) { let lastFunc; let lastRan; return function () { const context = this; const args = arguments; if (!lastRan) { func.apply(context, args); lastRan = Date.now(); } else { clearTimeout(lastFunc); lastFunc = setTimeout(() => { if (Date.now() - lastRan >= limit) { func.apply(context, args); lastRan = Date.now(); } }, limit - (Date.now() - lastRan)); } }; } function updateLayout() { console.log("Layout updated"); } const throttledUpdate = throttle(updateLayout, 200); window.addEventListener("resize", throttledUpdate);
In this example, the layout update function will only be called once every 200ms during window resizing.
In React, we can use custom hooks to make the debounce and throttle functionality reusable across components. This enhances modularity and optimizes performance in various interactions.
<p>import { useRef, useCallback } from "react";<br> const useDebounce = (func, delay) => {<br> const timer = useRef(null);<br> return useCallback(<br> (...args) => {<br> if (timer.current) {<br> clearTimeout(timer.current);<br> }<br> timer.current = setTimeout(() => func(...args), delay);<br> },<br> [func, delay]<br> );<br> };<br> export default useDebounce;</p>
<p>import React, { useState } from "react";<br> import useDebounce from "./useDebounce";<br> const SearchComponent = () => {<br> const [searchTerm, setSearchTerm] = useState("");</p> <p>const fetchResults = (query) => {<br> console.log(Fetching results for </span><span class="p">${</span><span class="nx">query</span><span class="p">}</span><span class="s2">);<br> return new Promise((resolve) => setTimeout(resolve, 1000));<br> };<br> const debouncedFetch = useDebounce(fetchResults, 300);<br> const handleSearch = (e) => {<br> setSearchTerm(e.target.value);<br> debouncedFetch(e.target.value);<br> };<br> return <input value={searchTerm} onChange={handleSearch} placeholder="Search..." />;<br> };<br> export default SearchComponent;</p>
<p>import { useRef, useCallback } from "react";<br> const useThrottle = (func, limit) => {<br> const lastRun = useRef(Date.now());<br> return useCallback(<br> (...args) => {<br> const now = Date.now();<br> if (now - lastRun.current >= limit) {<br> func(...args);<br> lastRun.current = now;<br> }<br> },<br> [func, limit]<br> );<br> };<br> export default useThrottle;</p>
<p>import React, { useEffect } from "react";<br> import useThrottle from "./useThrottle";</p> <p>const ScrollComponent = () => {<br> const handleScroll = () => {<br> console.log("Scrolled!");<br> };<br> const throttledScroll = useThrottle(handleScroll, 500);<br> useEffect(() => {<br> window.addEventListener("scroll", throttledScroll);<br> return () => window.removeEventListener("scroll", throttledScroll);<br> }, [throttledScroll]);<br> return <div style={{ height: "200vh" }}>Scroll down to see the effect</div>;<br> };<br> export default ScrollComponent;</p>
Both debouncing and throttling are indispensable techniques to enhance performance in modern applications. While debouncing is ideal for inputs like search fields, throttling is best suited for high-frequency events like scrolling. Custom hooks in React, like useDebounce and useThrottle, make these optimizations easy to implement across your app, ensuring a more efficient, responsive experience.
The above is the detailed content of Enhancing Performance in JavaScript: Understanding Debouncing and Throttling. For more information, please follow other related articles on the PHP Chinese website!