#1005 ✓duplicate
colin scott

Optimize #bind

Reported by colin scott | March 3rd, 2010 @ 04:44 PM

We needed a blinding fast bind method for a recent project the old one was too slow. After investigation it was discovered that prototypes bind method would never be fast enough for our project so long as the inner function references the arguments keyword.

So, we extended prototype with a bindFast method. This method runs 5 to 10 times faster in IE8, FF3.6 and CHROME 4 than the original bind. The downside of the bindFast method is that it can only support a relatively small numbers of arguments.
However, is there anyway that an implementation like this could make it into Prototype given this limitation? Would anyone ever pass more than say.. 10 arguments to a function? I don't think I've ever passed more than 5!
I've attached a test suite. It needs to be run from the same directory as prototype.js.

Function.prototype.bindFast = function(context) {
    if(arguments.length < 2 && Object.isUndefined(arguments[0])) return this;
    var aa;
    if (arguments.length < 2)
        aa = [];
    else
        aa = Array.prototype.slice.call(arguments, 1);
    var fn = this;
    return function(a1, a2, a3, a4, a5, a6, a7, a8) {
        switch(aa.length){
        case 0:
            return fn.call(context, a1, a2, a3, a4, a5, a6, a7, a8);
        case 1:
            return fn.call(context, aa[0], a1, a2, a3, a4, a5, a6, a7, a8);
        case 2:
            return fn.call(context, aa[0], aa[1], a1, a2, a3, a4, a5, a6, a7, a8);
        case 3:
            return fn.call(context, aa[0], aa[1], aa[2], a1, a2, a3, a4, a5, a6, a7, a8);
        case 4:
            return fn.call(context, aa[0], aa[1], aa[2], aa[3], a1, a2, a3, a4, a5, a6, a7, a8);
        case 5:
            return fn.call(context, aa[0], aa[1], aa[2], aa[3], aa[4],a1, a2, a3, a4, a5, a6, a7, a8);
        case 6:
            return fn.call(context, aa[0], aa[1], aa[2], aa[3], aa[4], aa[5], a1, a2, a3, a4, a5, a6, a7, a8);
        case 7:
            return fn.call(context, aa[0], aa[1], aa[2], aa[3], aa[4], aa[5], aa[6], a1, a2, a3, a4, a5, a6, a7, a8);
        case 8:
            return fn.call(context, aa[0], aa[1], aa[2], aa[3], aa[4], aa[5], aa[6], aa[7], a1, a2, a3, a4, a5, a6, a7, a8);
        }
    }
}

Comments and changes to this ticket

  • Tobie Langel

    Tobie Langel March 3rd, 2010 @ 05:08 PM

    • State changed from “new” to “duplicate”

    This issue has already been reported here: #599

    The different solutions proposed don't have similar limitations and will be favored.

    We're closing this ticket as duplicate. Thanks for taking the time to contribute.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

The Prototype JavaScript library.

Shared Ticket Bins

People watching this ticket

Attachments

Pages