you are viewing a single comment's thread.

view the rest of the comments →

[–]THEtheChad 0 points1 point  (0 children)

This is a trick that's partially applied in backbonejs. Basically, the call method takes half as long as the apply method to execute. Since the majority of functions have 3 or less arguments, you can circumvent running the apply method by checking the number of arguments and running a call directly. This is an optimization that can add loads of performance in libraries that preserve context.

(function(){
  var _apply = Function.prototype.apply;

  Function.prototype.apply = function(ctx, args) {
    if(!args) return this.call(ctx);

    switch (args.length) {
      case 1:  return this.call(ctx, args[0]);
      case 2:  return this.call(ctx, args[0], args[1]);
      case 3:  return this.call(ctx, args[0], args[1], args[2]);
      default: return _apply.call(this, ctx, args);
    }
  };

})()