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); bind: function() { > if (arguments.length < 2 && Object.isUndefined(arguments[0])) return > this; > var __method = this, args = $A(arguments), object = args.shift(); > return function() { > return __method.apply(object, args.concat($A(arguments))); > } > } So in all cases, it's less overhead to just not use .bind(). It's purely a convenience method. Ryan Gahl CEO Nth Penguin, LLC http://www.nthpenguin.com -- Inquire: 1-920-574-2218 Blog: http://www.someElement.com LinkedIn Profile: http://www.linkedin.com/in/ryangahl On Tue, Mar 10, 2009 at 4:53 PM, kangax <kan...@gmail.com> wrote: > > On Mar 10, 3:27 pm, "T.J. Crowder" <t...@crowdersoftware.com> wrote: > > > I do agree there are just times when .bind() is called for, but I am > fairly > > > certain the majority of people WAY overuse it. > > > > +1 to that, where you're defining the function inline. Where you're > > not, while this may be faster: > > > > var self = this; > > handler = function() { > > self.realHandler(); > > } > > > > ...bind is (for me) much clearer: > > > > handler = realHandler.bind(this); > > > > But again, yeah, when you're defining the function inline _anyway_, > > take advantage of the native closure stuff. > > I absolutely agree that not using `bind` with local function works > well and works fast. I also agree that `bind` is overused. What I want > to stress, though, is that when binding a non-local function, `bind` > actually *eliminates an extra closure* that would otherwise be > implicitly created. You either save on memory, or on runtime > performance: > > 1) Bound event handler is augmented and is slower than original one > (at runtime), as it has to go through function call, `apply` > invocation and even worse - `$A` normalization, etc. (in older > versions of prototype) > > this.element.observe('click', this.onClick.bind(this)); > > > 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) > > var self = this; > this.element.observe('click', function(){ self.onClick() }); > > [...] > > -- > 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 prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~----------~----~----~----~------~----~------~--~---