Improving Function.prototype.bind

François REMY fremycompany_pub at
Fri Jan 6 01:29:27 PST 2012

Such an implementation would be very slow and not efficient, because 
searching for a key in the "boundFunctions" weakmap would take time. It can 
be a polyfill solution but a new implementation need something better.

I think the solution would be to create an "invisible" [[boundFunctions]] 
property for all objects containing a dictionnary<weak<function>, 
weak<boundFunction>> of already-bound functions on the object. The 
implementation of bind would then be :

- if objToBind is not a reference, create a new boundFunction and return it.
- if objToBind.[[boundFunctions]] don't exist, create it.
- else, check if it contains a key for functionToBind //when you are 
processing the dictionnary, remove obsolete entries you may find
    - if yes, check if the weak reference is alive
    - if yes, returns the boundFunction
- create a new boundFunction and store it into objToBind.[[boundFunctions]]
- return the newly created boundFunction

Would that be possible?


-----Message d'origine----- 
From: Brendan Eich
Sent: Friday, January 06, 2012 2:22 AM
To: Andrea Giammarchi
Cc: Axel Rauschmayer ; François REMY ; es-discuss
Subject: Re: Improving Function.prototype.bind

On Jan 5, 2012, at 4:47 PM, Andrea Giammarchi wrote:

> Guys, by any chance we can go back into the topic?

You'll have to polyfill Function.prototype.bind in the current world of 
pre-ES5 browsers. Why not then go on to wrap it in a memoizing version that 
uses a WeakMap if available (emulated with strong keys/values array-based 
implementation if not)?

If you need this machinery, it's all doable. If we should evolve a Harmony 
bind or betterBind based on your experience and others, we can do that.

Right now the lack of memoization is not a burning issue, from what I hear. 
You've raised it, I think for the first time on es-discuss.


More information about the es-discuss mailing list