On Mar 11, 11:29 am, Robert Kieffer <bro...@gmail.com> wrote:
[...]
> @kangax, re: use of $A, shift, apply - Sure, there's room for other
> performance tweaks, but what I care most about is the performance of
> invoking a bound function, not in the construction of a bound
> function.  This is why the function that I return in my implementation

Of course runtime performance is the key here. That's why the patch in
#215 forks 4 times (twice at load time) based on whether partial
application needs to be performed. In my experience, most of the time,
bind is used *without* partial application (which was the main reason
I wrote the patch). When invoking bind without arguments, a much
faster version is returned. That faster version in its turn *also*
forks twice. Why? Because we know that `fn.apply(context, arguments)`
is slower that `fn.call(context)` (at least in Firefox, not sure about
other clients). That's why we use latter version when
`arguments.length` (of bound function) is 0. This exact (partialless)
optimization is what I was aiming for. I added my implementation to
your test right after Tobie's one:

var bind3 = (function(){
  var _slice = Array.prototype.slice;
  return function(context) {
    var fn = this,
        args = _slice.call(arguments, 1);
    if (args.length) {
      return function() {
        return arguments.length
          ? fn.apply(context, args.concat(_slice.call(arguments)))
          : fn.apply(context, args);
      }
    }
    return function() {
      return arguments.length
        ? fn.apply(context, arguments)
        : fn.call(context);
    };
  }
})();

then called it like so: - `testBindFunction('kangax', bind3);`

The results are: http://tinyurl.com/bgbj5k

You can see that `bind3` outperforms every other opponent by a great
margin. Second time I ran the test, your version outperformed mine,
when partially applying (the one that's - "obj, 1, 2"). I would want
you to test these too and see which results you are getting. As far as
I can see, you have a good demonstration here of how slow `concat` is
and how manual concatenation can speed things up. It's a nice
optimization of a partial application case, but as you can see, we can
do better in other (more common?) cases.

When Tobie was rewriting functional extensions, I proposed using
approach similar to `bind3` [1], but for whatever reason it never
happened : )

> inlines everything except the one apply() call.  I suspect the code
> you refer to (at the bottom of bug #215) would benefit from the
> strategy I use to recycle the args array and marshal the bound args
> and passed in arguments together as part of the inline code.

[...]

[1] 
http://github.com/sstephenson/prototype/commit/d55932fc8c60847406789fa57c998c6ec6c46385#comments

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

Reply via email to