Delegate Object Test

JavaScript performance comparison

Test case created by James Wilkins

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var d = (function() {
   
      function Delegate(obj, func) {
        this.obj = obj;
        this.func = func;
        this.functext = func.toString();
        this.bindinvoke = func.bind(obj);
        var _this = obj; // (note: putting this in 'eval()' will slow it down!)
        this.evalinvoke = obj.constructor.prototype['test'] = eval("(" + func + ")");
        obj.constructor.prototype[this.functext] = this.evalinvoke;
        this.funcinvoke = Function("_this", "return (" + func + ")");
      }
      Delegate.prototype.call = function() {
        return this.func.apply(this.obj, arguments);
      };
      Delegate.prototype.apply = function(args) {
        return this.func.apply(this.obj, args);
      };
      Delegate.prototype.fastcall = function() {
        switch (arguments.length) {
          0: return this.evalinvoke();
          1: return this.evalinvoke(arguments[0]);
          2: return this.evalinvoke(arguments[0], arguments[1]);
          3: return this.evalinvoke(arguments[0], arguments[1], arguments[3]);
          default: return this.func.apply(this.obj, arguments);
        }
      };
      var o = {
        x: 1
      };
      var d = new Delegate(o, function test() {
        return (typeof _this != 'undefined') ? _this.x : this.x;
      });
   
      return d;
    })();
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
call
d.call()
pending…
apply
d.apply()
pending…
bind invoke
d.bindinvoke();
pending…
eval invoke
d.evalinvoke();
pending…
func invoke
d.funcinvoke();
pending…
direct on object
d.obj.test();
pending…
direct on obj using index
d.obj[d.functext]();
pending…
fastcall 0 params
d.fastcall();
pending…
fastcall 3 params
d.fastcall(1, 2, 3);
pending…
fastcall 5 params
d.fastcall(1, 2, 3, 4, 5);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment