On Mar 10, 6:12 pm, Ryan Gahl <[email protected]> wrote:
> Kangax...
>
> 2) Non-bound event handler is faster at run time, as it accesses
>
> > `onClick` from the proper object directly, but presumably consumes
> > more memory (due to extra closure, created during function expression
> > evaluation)
>
> There is no "extra" closure because .bind() creates a closure anyway
> (because just calling .apply would execute the function right away);

Good point, Ryan. A function is created in both cases and in both
cases it always carries a closure; After all, it needs to keep a
`context` somewhere : )

I guess what I wanted to say is that a function created with
Prototype's `bind` (internally) has a predictable closure - the one
that's got formed when a function was declared. And it is declared by
Prototype, inside a very "slim" scope (self-executing function,
surrounding `Function.prototype` extensions). That scope has no heavy
objects, no host objects (!) and so when bind internally creates a
function, that function closure consumes a relatively small amount of
memory.

Now, what happens if you `bind` manually (as in my second example) and
you're in the middle of some method with bunch of host objects, maybe
some large objects, etc.? What if that method is deeply nested
(although that's probably an indication of a bad design :)). Such
closure can obviously consume quite a lot, as all that stuff is being
closed over in some event handler that you're binding.

Don't forget that having "inline" function is not always feasible. In
such cases, using `bind` can be safer.

[...]

--
kangax
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to