Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Lodash memoizeDebounce and throttle extensions
import { debounce, throttle } from 'lodash';
import memoize from 'memoizee';
// Memoize to use unique args, and debounce to only get the last call
// https://github.com/lodash/lodash/issues/2403#issuecomment-290760787
export const memoizeDebounce = (func, wait = 0, options = {}) => {
const { leading, maxWait, trailing, ...memoizeOptions } = options;
const mem = memoize(
() => debounce(func, wait, { leading, maxWait, trailing }),
memoizeOptions
);
return (...args) => {
mem(...args)(...args);
};
};
// Memoize to use unique args, and debounce to only get the last call
// https://github.com/lodash/lodash/issues/2403#issuecomment-290760787
export const memoizeThrottle = (func, wait = 0, options = {}) => {
const { leading, trailing, ...memoizeOptions } = options;
const mem = memoize(
() => throttle(func, wait, { leading, trailing }),
memoizeOptions
);
return (...args) => {
mem(...args)(...args);
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.