custom bind

JavaScript performance comparison

Revision 4 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var Foo = function(a) {
      return this.v + a
    };
    var FooProps = function(v) {
      this.v = v;
    }
    var PublicFooCustom = function(v) {
      var private = new FooProps(v);
      return function() {
        Foo.apply(private, arguments);
      };
    }
    var PublicFooBound = function(v) {
      var private = new FooProps(v);
      return Foo.bind(private);
    }
   
    var Goo = function(v) {
      this.v = v;
    }
    Goo.prototype._ = function(a) {
      return this.v + a;
    }
   
    var Moo_TOKEN = {};
   
    var Moo_Sub = function(a) {
      this.v = a;
    }
    var Moo = function(a) {
      var p = new Moo_Sub(a);
      this.p = function(k) {
        return k === Moo_TOKEN ? p : undefined;
      }
    }
   
    custom = [];
    for (var i = 0; i < 1e4; i++)
      custom.push(PublicFooCustom(i));
   
    bound = []
    for (var i = 0; i < 1e4; i++)
      bound.push(PublicFooBound(i));
   
    newed = []
    for (var i = 0; i < 1e4; i++)
      newed.push(new Goo(i));
   
    mood = [];
    for (var i = 0; i < 1e4; i++)
      mood.push(new Moo(i));
};
</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
for (var i = 0, t = 0; i < 1e4; i++)
  t += bound[i](i);
pending…
Custom
for (var i = 0, t = 0; i < 1e4; i++)
  t += custom[i](i);
pending…
New
for (var i = 0, t = 0; i < 1e4; i++)
  t += newed[i]._(i);
pending…
Moo
for (var i = 0, t = 0; i < 1e4; i++)
  t += mood[i].p(Moo_TOKEN).v + i;
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