Home >Web Front-end >JS Tutorial >Decreasing server load by using debouncing/throttle technique in reactjs
Debouncing and throttling are techniques used to optimize performance by controlling the frequency of function execution in response to frequent events (e.g., typing, scrolling, resizing).
Debouncing delays the execution of a function until after a certain amount of time has passed since the last time it was invoked.
import React, { useState, useEffect } from 'react'; const DebouncedSearch = () => { const [searchTerm, setSearchTerm] = useState(''); const [debouncedValue, setDebouncedValue] = useState(''); useEffect(() => { const handler = setTimeout(() => { setDebouncedValue(searchTerm); }, 500); // Delay of 500ms return () => { clearTimeout(handler); // Cleanup on input change }; }, [searchTerm]); useEffect(() => { if (debouncedValue) { console.log('API Call with:', debouncedValue); // Make API call here } }, [debouncedValue]); return ( <input type="text" placeholder="Search..." value={searchTerm} onChange={(e) => setSearchTerm(e.target.value)} /> ); }; export default DebouncedSearch;
Throttling ensures that a function is executed at most once in a specified time interval, regardless of how many times it is triggered during that interval.
import React, { useEffect } from 'react'; const ThrottledScroll = () => { useEffect(() => { const handleScroll = throttle(() => { console.log('Scroll event fired'); }, 1000); // Execute at most once every 1000ms window.addEventListener('scroll', handleScroll); return () => { window.removeEventListener('scroll', handleScroll); }; }, []); return <div> <hr> <h3> <strong>3. Using Libraries (Optional)</strong> </h3> <p>Instead of implementing custom debounce/throttle, you can use popular libraries like:</p> <h4> <strong>Lodash</strong> </h4> <p>Install:<br> </p> <pre class="brush:php;toolbar:false">npm install lodash
Usage:
import { debounce, throttle } from 'lodash'; // Debounce Example const debouncedFunc = debounce(() => console.log('Debounced!'), 500); // Throttle Example const throttledFunc = throttle(() => console.log('Throttled!'), 1000);
Install:
npm install react-use
Usage:
import { useDebounce, useThrottle } from 'react-use'; const Demo = () => { const [value, setValue] = useState(''); const debouncedValue = useDebounce(value, 500); const throttledValue = useThrottle(value, 1000); useEffect(() => { console.log('Debounced:', debouncedValue); console.log('Throttled:', throttledValue); }, [debouncedValue, throttledValue]); return ( <input value={value} onChange={(e) => setValue(e.target.value)} placeholder="Type something..." /> ); }; export default Demo;
|
Debouncing |
Throttling | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Execution |
Executes once after the user stops firing events for a specified time. | Executes at regular intervals during the event. | ||||||||||||
Search input, resizing, form validation. | Scroll events, button clicks, API polling. | |||||||||||||
Performance | Reduces the number of function calls. | Limits execution to once per interval. |
The above is the detailed content of Decreasing server load by using debouncing/throttle technique in reactjs. For more information, please follow other related articles on the PHP Chinese website!