Delegate Object Test

JavaScript performance comparison

Revision 2 of this test case created by James Wilkins and last updated

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.apply = function(args) {
        var _this = this;
        if (args === void 0 || !args.length) return _this.evalinvoke();
        switch (args.length) {
          case 0:
            return _this.evalinvoke();
          case 1:
            return _this.evalinvoke(args[0]);
          case 2:
            return _this.evalinvoke(args[0], args[1]);
          case 3:
            return _this.evalinvoke(args[0], args[1], args[2]);
          default:
            return _this.func.apply(this.obj, args);
        }
      };
      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
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…
apply
d.apply();
pending…
apply empty array
d.apply([]);
pending…
apply 1 params
d.apply([1]);
pending…
apply 2 params
d.apply([1, 2]);
pending…
apply 3 params
d.apply([1, 2, 3]);
pending…
apply 5 params
d.apply([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