Skip to content

Instantly share code, notes, and snippets.

@leontastic
Created March 29, 2019 09:24
Show Gist options
  • Save leontastic/6187abde255bd5f93dfef8996d8080d7 to your computer and use it in GitHub Desktop.
Save leontastic/6187abde255bd5f93dfef8996d8080d7 to your computer and use it in GitHub Desktop.
memoizer that only remembers the most recent input
// memoizer that only remembers the most recent input
const memoizeOne = <Arguments extends any[], Output = any>(
fn: (...args: Arguments) => Output,
resolver = (...args: Arguments) => args[0],
) => {
let resolution: any;
let value: Output;
return (...args: Arguments) => {
const newResolution = resolver(...args);
if (!resolution || resolution !== newResolution) {
resolution = newResolution;
value = fn(...args);
}
return value;
};
};
export default memoizeOne;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment