Improving Function.prototype.bind

David Bruant bruant.d at gmail.com
Fri Jan 6 02:41:56 PST 2012


Le 06/01/2012 11:20, Andrea Giammarchi a écrit :
>
> On Fri, Jan 6, 2012 at 10:50 AM, David Bruant <bruant.d at gmail.com 
> <mailto:bruant.d at gmail.com>> wrote:
>
>
>     Your proposal won't be implemented in older browsers. Actually, it
>     is very likely that your proposal would be implemented in browsers
>     that would already have weak maps.
>     Under these conditions. What is the benefit of a native
>     implementation rather than an WeakMap based polyfill?
>
>     David
>
>     [1]
>     http://code.google.com/p/es-lab/source/browse/trunk/src/ses/WeakMap.js
>
>
>
>
> Why would you say that ? This version https://gist.github.com/1569978 
> should works in every browser without problems.
I meant that your proposal won't be implemented *natively* by older 
browsers. Sorry for the confusion.


>
> I have also tested memory leaks via snapshots through the profiler ...
>
> // snapshot, 1.2 Mb
>
> function test(){}
> for (var i = 0, a = []; i < 0xFFFF; i++) a.push({}.boundTo(test));
>
> // snapshot 35.8 Mb
> a = null;
>
> // snapshot, 1.2 Mb
>
> And no WeakMap is used ... am I missing something ?
In your implementation, you store in an array references to functions, 
not to objects.

I would guess that
-----
var o = {};
for (var i = 0, a = []; i < 0xFFFF; i++) a.push(o.boundTo(function(){}));
a = null;
-----
leaks. Does it?

I certainly leaks, because references to the functions accumulate in 
'cbStack' and are never free'd.

Having an own "boundTo" property in every single object saves you from a 
memory leak in the snippet you wrote above... at the cost of potential 
collisions (and an abstraction leak). And it would be an enumerable 
property in an ES3 browser.
I would prefer the extra memory leak in this case, but that's a matter 
of taste.

David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120106/4bee6a1a/attachment.html>


More information about the es-discuss mailing list