Skip to content

Instantly share code, notes, and snippets.

@dounan
Last active October 18, 2017 11:36
Show Gist options
  • Save dounan/bde4439f77077f4969c8970f768b736b to your computer and use it in GitHub Desktop.
Save dounan/bde4439f77077f4969c8970f768b736b to your computer and use it in GitHub Desktop.
Memoized bind implementation and example
function memoizedBind(fn, ...args) {
// Memoize the bind call (with cache size 1).
if (!areArgumentsShallowlyEqual(fn.__lastArgs, args)) {
fn.__lastResult = fn.bind.apply(fn, args);
fn.__lastArgs = args;
}
return fn.__lastResult;
}
// ======================================================
// Before transform
myFn.bind(this, foo, bar);
// ======================================================
// After transform
memoizedBind(myFn, this, foo, bar);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment