Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
throttle-debounce
Advanced tools
The throttle-debounce npm package provides utility functions to throttle and debounce function calls. Throttling ensures that a function is not called more often than the specified delay, while debouncing ensures that a function is only called after a certain amount of time has passed without it being called again.
throttle
Throttling allows you to limit the number of times a function can be executed over time. In this example, the 'throttledFunction' will only be called at most once every 300 milliseconds, even if the 'resize' event fires more frequently.
import { throttle } from 'throttle-debounce';
const throttledFunction = throttle(300, (arg) => {
console.log('Throttled:', arg);
});
window.addEventListener('resize', () => throttledFunction(window.innerWidth));
debounce
Debouncing allows you to delay the execution of a function until a certain amount of time has passed without it being called. In this example, the 'debouncedFunction' will only be called 300 milliseconds after the last 'keyup' event is fired.
import { debounce } from 'throttle-debounce';
const debouncedFunction = debounce(300, (arg) => {
console.log('Debounced:', arg);
});
document.getElementById('input').addEventListener('keyup', () => debouncedFunction(document.getElementById('input').value));
Lodash is a popular utility library that includes throttle and debounce functions among many other utilities. It is more comprehensive than throttle-debounce but also larger in size, which might be a consideration for projects concerned with minimizing dependencies.
Underscore.js is another utility library that provides similar functionality to Lodash, including throttle and debounce functions. It is often considered as a lighter alternative to Lodash, although it has fewer features.
The debounce package is a minimalistic library focused solely on the debounce functionality. It is smaller than throttle-debounce but does not provide a throttle function.
Bottleneck is a rate limiter that can be used to throttle function calls. It is more complex and allows for more fine-grained control over how function calls are queued and executed, compared to the simpler throttle function provided by throttle-debounce.
Throttle and debounce functions.
This module is the same as jquery-throttle-debounce (with some differences), but it’s transferred to ES Modules and CommonJS format.
npm install throttle-debounce --save
throttle
import { throttle } from 'throttle-debounce';
const throttleFunc = throttle(
1000,
(num) => {
console.log('num:', num);
},
{ noLeading: false, noTrailing: false }
);
// Can also be used like this, because noLeading and noTrailing are false by default
const throttleFunc = throttle(1000, (num) => {
console.log('num:', num);
});
throttleFunc(1); // Will execute the callback
throttleFunc(2); // Won’t execute callback
throttleFunc(3); // Won’t execute callback
// Will execute the callback, because noTrailing is false,
// but if we set noTrailing to true, this callback won’t be executed
throttleFunc(4);
setTimeout(() => {
throttleFunc(10); // Will execute the callback
}, 1200);
// Output
// num: 1
// num: 4
// num: 10
debounce
import { debounce } from 'throttle-debounce';
const debounceFunc = debounce(
1000,
(num) => {
console.log('num:', num);
},
{ atBegin: false }
);
// Can also be used like this, because atBegin is false by default
const debounceFunc = debounce(1000, (num) => {
console.log('num:', num);
});
// Won’t execute the callback, because atBegin is false,
// but if we set atBegin to true, this callback will be executed.
debounceFunc(1);
debounceFunc(2); // Won’t execute callback
debounceFunc(3); // Won’t execute callback
// Will execute the callback,
// but if we set atBegin to true, this callback won’t be executed.
debounceFunc(4);
setTimeout(() => {
debounceFunc(10); // Will execute the callback
}, 1200);
// Output
// num: 4
// num: 10
Debounce and throttle can both be cancelled by calling the cancel
function.
const throttleFunc = throttle(300, () => {
// Throttled function
});
throttleFunc.cancel();
const debounceFunc = debounce(300, () => {
// Debounced function
});
debounceFunc.cancel();
The logic that is being throttled or debounced will no longer be called.
To cancel only one upcoming debounced call, you can pass upcomingOnly: true
option to cancel
function:
const debounceFunc = debounce(300, () => {
// Debounced function
});
debounceFunc(); // will not be invoked
debounceFunc.cancel({ upcomingOnly: true });
debounceFunc(); // will be invoked
Returns: Function
Throttle execution of a function. Especially useful for rate limiting execution of handlers on events like resize and scroll.
Type: Number
A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.
Type: Function
A function to be executed after delay milliseconds. The this
context and all
arguments are passed through, as-is, to callback
when the throttled-function
is executed.
Type: Boolean
Optional, defaults to false. If noTrailing is true, callback will only execute
every delay
milliseconds while the throttled-function is being called. If
noTrailing is false or unspecified, callback will be executed one final time
after the last throttled-function call. (After the throttled-function has not
been called for delay
milliseconds, the internal counter is reset)
Type: Boolean
Optional, defaults to false. If noLeading is false, the first throttled-function call will execute callback immediately. If noLeading is true, the first the callback execution will be skipped. It should be noted that callback will never executed if both noLeading = true and noTrailing = true.
Type: Boolean
If debounceMode
is true (at begin), schedule clear
to execute after delay
ms. If debounceMode
is false (at end), schedule callback
to execute after
delay
ms.
Returns: Function
Debounce execution of a function. Debouncing, unlike throttling, guarantees that a function is only executed a single time, either at the very beginning of a series of calls, or at the very end.
Type: Number
A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.
Type: Function
A function to be executed after delay milliseconds. The this
context and all
arguments are passed through, as-is, to callback
when the debounced-function
is executed.
Type: Boolean
Optional, defaults to false. If atBegin
is false or unspecified, callback will
only be executed delay
milliseconds after the last debounced-function call. If
atBegin
is true, callback will be executed only at the first
debounced-function call. (After the throttled-function has not been called for
delay
milliseconds, the internal counter is reset).
$.throttle
and
$.debounce
to be availableTested in Chrome 72, Edge 15, Firefox 65 and should work in all modern browsers (support based on Browserslist configuration).
For automated tests, run npm run test:automated
(append :watch
for watcher
support).
MIT © Ivan Nikolić
[5.0.2][] - 2024-06-24
FAQs
Throttle and debounce functions.
The npm package throttle-debounce receives a total of 4,188,618 weekly downloads. As such, throttle-debounce popularity was classified as popular.
We found that throttle-debounce demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.