call vs apply vs Ember.apply (v73)

Revision 73 of this benchmark created on


Setup

function Obj() {
      this.that = 'that';
    }
    
    Obj.prototype.target = function() {
      return 'that: ' + this.that + ', args: ' + Array.prototype.slice.call(arguments).join(' ,');
    };
    
    function EmApply(t /* target */ , m /* method */ , a /* args */ ) {
      var l = a && a.length;
      if (!a || !l) {
        return m.call(t);
      }
      switch (l) {
        case 1:
          return m.call(t, a[0]);
        case 2:
          return m.call(t, a[0], a[1]);
        case 3:
          return m.call(t, a[0], a[1], a[2]);
        case 4:
          return m.call(t, a[0], a[1], a[2], a[3]);
        case 5:
          return m.call(t, a[0], a[1], a[2], a[3], a[4]);
        default:
          return m.apply(t, a);
      }
    }
    
    var myObj = new Obj();

Test runner

Ready to run.

Testing in
TestOps/sec
Call - 1 Argument
Obj.prototype.target.call(myObj, 'arg1');
ready
Apply - 1 Argument
Obj.prototype.target.apply(myObj, ['arg1']);
ready
Ember Apply
EmApply(myObj, Obj.prototype.target, ['arg1']);
ready
Normal invocation
myObj.target('arg1');
ready
Call - 4 Arguments
Obj.prototype.target.call(myObj, 'arg1', 'arg2', 'arg3', 'arg4');
ready
Apply - 4 Arguments
Obj.prototype.target.apply(myObj, ['arg1', 'arg2', 'arg3', 'arg4']);
ready
Ember Apply - 4 Arguments
EmApply(myObj, Obj.prototype.target, ['arg1', 'arg2', 'arg3', 'arg4']);
ready
Normal - 4 Arguments
myObj.target('arg1', 'arg2', 'arg3', 'arg4');
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.