Skip to content

Instantly share code, notes, and snippets.

@vitalipe
Created June 1, 2017 11:12
Show Gist options
  • Select an option

  • Save vitalipe/cd28ea7d3b75c3b37f46385cd0080441 to your computer and use it in GitHub Desktop.

Select an option

Save vitalipe/cd28ea7d3b75c3b37f46385cd0080441 to your computer and use it in GitHub Desktop.
// calling this version of bind inside .render() should only create each bound function once
// WARNING: I didn't even ran this code :P so I'm not sure if it works
const _cache = new WeakMap();
const _cmpArray = (arr1, arr2) => (arr1.length === arr2.length) &&
(arr1.every((v,i)=> (v === arr2[i])));
function bind(fn, context, ...args) {
if (!_cache.has(context))
_cache.set(context, new Map());
let componentCache = _cache.get(context);
if (!componentCache.has(fn))
componentCache.set(fn, [[args, fn.bind(context, ...args)]]);
let fragments = componentCache.get(fn);
let fragment = fragments.find(([boundArgs]) => _cmpArray(boundArgs, args))
if (fragment)
return fragment[1];
fragment = [args, fn.bind(context, ...args)];
fragments.push(fragment)
return fragment[1];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment