Hi again,

Sorry for the double-post, somehow I managed to forget to say: My
mechanism isn't just (slightly) more long-winded, it's also more
complex to understand -- it demands more of the programmer. That's its
real downside, not the trivial difference in the length of

    method.$super.call(this, arg);

vs.

    $super(arg);

To use my mechanism, you have to understand how JavaScript functions
and the `this` keyword work, and it's very easy to make mistakes like
this:

    method.$super(arg);

...which fails when the parent class's code tries to use `this`.

So it's about trade-offs. Prototype's mechanism is more newbie-
friendly, and there's real strength in that. My mechanism is
dramatically more efficient, and quite easy to use once you understand
the language a bit better, but not for newbies.

-- T.J. :-)

On Mar 18, 4:01 pm, "T.J. Crowder" <t...@crowdersoftware.com> wrote:
> Hi Ryan,
>
> > Just want to point out that the "marked runtime cost" is only at class
> > definition time, not instance creation nor method call time.
>
> Actually, it's every single time anything calls a method that has the `
> $super` argument (whether the `$super` argument gets used or not).
> Every single call, there are multiple overhead function calls and a
> function *creation*. Here's what happens:
>
> 1. The call to your method is actually to a Prototype wrapper for your
> method
> 2. The wrapper calls `Function#bind` (yes, every time)
> 3. `bind` calls `Array#slice`, because `bind` handles arguments
> (although it doesn't in this case)
> 4. `bind` creates and returns a new function
> 5. The wrapper calls its internal `update` function to update the
> arguments
> 6. The wrapper calls your actual method via `apply`
>
> If you actually *use* `$super`, then there are about five extra
> function calls involved before the parent method actually gets called
> (but no new functions get created).
>
> So the runtime costs are significant, not just at `Class.create` time
> (decompiling every public function in your class to find out whether
> it has a `$super` argument), but also on each and every call to a
> method with a `$super` argument (several additional function calls,
> including creating new function objects).
>
> Probably *at least* 95% of the time you *don't care*, but still, I was
> pretty shocked when I found out what it was doing, which is what lead
> me to thinking if there was a better way. My mechanism doesn't do any
> function decompilation, doesn't create any functions when your methods
> are called, and introduces *no* extra calls at all [a call to one of
> your methods goes straight to your method, and your call to the parent
> method goes straight to the parent method (unless you use an optional
> helper function)]. But the notation is a bit longer. ;-)
>
> How much longer? If you use proper named functions, the call can be
>
>     method.$super.call(this, arg);
>
> ...compared with Prototype's:
>
>     $super(arg);
>
> If you use anonymous functions (`method: function(arg) { ... }`), then
> it's
>
>     this.method.$super.call(this, arg);
>
> I don't use anonymous functions, so I get the shorter one.
>
> -- T.J.  :-)
>
> On Mar 18, 2:39 pm, Ryan Gahl <ryan.g...@gmail.com> wrote:
>
>
>
>
>
>
>
> > On Fri, Mar 18, 2011 at 9:30 AM, T.J. Crowder 
> > <t...@crowdersoftware.com>wrote:
>
> > > Prototype's magical `$super` comes at a marked runtime cost
>
> > Just want to point out that the "marked runtime cost" is only at class
> > definition time, not instance creation nor method call time. So yeah, the
> > performance thing is a non issue.

-- 
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.

Reply via email to