call vs apply vs direct vs closure direct (v22)

Revision 22 of this benchmark created by James Wilkins on


Description

Test "call()" vs "apply()" vs calling functions directly (with and without closure).

Setup

function Obj() {
      this.that = "that";
      var _this = this;
      this.directClosure = function(v) {
        _this.that = v;
      };
    }
    Obj.prototype.test = function(val) {
      this.that = val;
    };
    Obj.prototype.wrapFunc = function(func) {
      var _this = this;
      this.invokeWrappedFunc = eval("(" + func + ")");
    }
    Obj.prototype.wrapFunc2 = function(func) {
      this.invokeWrappedFunc2 = new Function("_this", "return (" + func + ")");
    }
    var o = new Obj();
    o.wrapFunc(function(v) {
      _this.that = v;
    });
    o.wrapFunc2(function(v) {
      _this.that = v;
    });

Test runner

Ready to run.

Testing in
TestOps/sec
apply
Obj.prototype.test.apply(o, ["YES"]);
ready
call
Obj.prototype.test.call(o, "YES");
ready
direct
o.test("YES")
ready
Wrapped Func (eval)
o.invokeWrappedFunc("YES");
ready
Wrapped Func (new Function)
o.invokeWrappedFunc2("YES");
ready
Direct Closure
o.directClosure("YES");
ready

Revisions

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