Last active
October 18, 2017 11:36
-
-
Save dounan/bde4439f77077f4969c8970f768b736b to your computer and use it in GitHub Desktop.
Memoized bind implementation and example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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