r/javascript • • Mar 09 '15

Things every JavaScript developer should know

http://ilikekillnerds.com/2015/03/things-every-javascript-developer-should-know/
326 Upvotes

118 comments sorted by

View all comments

Show parent comments

2

u/cosinezero Mar 09 '15

Could you explain further in what ways bind/call/apply can affect performance?

Not quite sure I can explain the whys, but I can certainly show you the benchmarks with various ways of calling things: http://jsperf.com/javascript-bind-vs-function

Of interest to you is the poor performance of the very first test.

3

u/[deleted] Mar 09 '15

Thanks for this. All the more reason to avoid bind.

0

u/cosinezero Mar 09 '15

Oh, I can name you a few times it's worth using bind. Currying, for instance. But you should avoid bind unless you need it. Simply using it to permanently set scope is not a good usage; use an anonymous call (apparently not apply!!) for that.

It is strange that apply is slow; I would think that would be optimized better than call.

1

u/Sinistralis Mar 11 '15

Why would you use bind for currying?

function sub_curry(fn /*, variable number of args */) {
    var args = [].slice.call(arguments, 1);
    return function () {
        return fn.apply(this, args.concat(toArray(arguments)));
    };
}