Skip to content

Instantly share code, notes, and snippets.

@nmccready
Created February 25, 2019 16:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmccready/1ef5df1d0f455ecad5b2ce99f8d32f8b to your computer and use it in GitHub Desktop.
Save nmccready/1ef5df1d0f455ecad5b2ce99f8d32f8b to your computer and use it in GitHub Desktop.
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