Classes with Inner Function Calls (Instantiation)

Benchmark created by Sebastian Markbåge on


Preparation HTML

<script>
  var ClassA = function(state) {
   this.state = state;
  };
  ClassA.prototype = {
  
   publicMethod1: function(x) {
    return this.state == 'foo' && this.publicMethod2(x);
   },
  
   publicMethod2: function(x) {
    return this.state == 'foo' && this.publicMethod3(x);
   },
  
   publicMethod3: function(x) {
    return this.state == 'foo' && this.publicMethod4(x);
   },
  
   publicMethod4: function(x) {
    return this.state == x;
   }
  
  };
  
  var ClassB = function(state) {
  
   function privateMethod1(x) {
    return state == 'foo' && privateMethod2(x);
   };
  
   function privateMethod2(x) {
    return state == 'foo' && privateMethod3(x);
   };
  
   function privateMethod3(x) {
    return state == 'foo' && privateMethod4(x);
   };
  
   function privateMethod4(x) {
    return state == x;
   };
  
   this.publicMethod1 = privateMethod1;
   this.publicMethod2 = privateMethod2;
   this.publicMethod3 = privateMethod3;
   this.publicMethod4 = privateMethod4;
  
  };
  
  var ClassC = function(state) {
   this.state = state;
  };
  ClassC.prototype = (function() {
  
   function privateMethod1(x) {
    return this.state == 'foo' && privateMethod2.call(this, x);
   };
  
   function privateMethod2(x) {
    return this.state == 'foo' && privateMethod3.call(this, x);
   };
  
   function privateMethod3(x) {
    return this.state == 'foo' && privateMethod4.call(this, x);
   };
  
   function privateMethod4(x) {
    return this.state == x;
   };
  
   return {
  
    publicMethod1: privateMethod1,
    publicMethod2: privateMethod2,
    publicMethod3: privateMethod3,
    publicMethod4: privateMethod4
  
   };
  
  })();
  
  
  var ClassD = function(state) {
   this.state = state;
  };
  ClassD.prototype = (function() {
  
   function privateMethod1(self, x) {
    return self.state == 'foo' && privateMethod2(self, x);
   };
  
   function privateMethod2(self, x) {
    return self.state == 'foo' && privateMethod3(self, x);
   };
  
   function privateMethod3(self, x) {
    return self.state == 'foo' && privateMethod4(self, x);
   };
  
   function privateMethod4(self, x) {
    return self.state == x;
   };
  
   return {
  
    publicMethod1: function(x) {
     return privateMethod1(this, x);
    },
  
    publicMethod2: function(x) {
     return privateMethod2(this, x);
    },
  
    publicMethod3: function(x) {
     return privateMethod3(this, x);
    },
  
    publicMethod4: function(x) {
     return privateMethod4(this, x);
    }
  
   };
  
  })();
  
  var ClassE = function(state) {
   this.state = state;
  };
  
  ClassE.prototype = (function() {
  
   var state;
  
   function privateMethod1(x) {
    return state == 'foo' && privateMethod2(x);
   };
  
   function privateMethod2(x) {
    return state == 'foo' && privateMethod3(x);
   };
  
   function privateMethod3(x) {
    return state == 'foo' && privateMethod4(x);
   };
  
   function privateMethod4(x) {
    return state == x;
   };
  
   return {
  
    publicMethod1: function(x) {
     var stack = state;
     state = this.state;
     var result = privateMethod1(x);
     state = stack;
     return result;
    },
  
    publicMethod2: function(x) {
     var stack = state;
     state = this.state;
     var result = privateMethod2(x);
     state = stack;
     return result;
    },
  
    publicMethod3: function(x) {
     var stack = state;
     state = this.state;
     var result = privateMethod3(x);
     state = stack;
     return result;
    },
  
    publicMethod4: function(x) {
     var stack = state;
     state = this.state;
     var result = privateMethod4(x);
     state = stack;
     return result;
    }
  
   };
  
  })();
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Plain prototype
var inst = new ClassA('foo');
var result = inst.publicMethod1('foo')
result = inst.publicMethod2('foo');
result = inst.publicMethod3('foo');
result = inst.publicMethod4('foo');
ready
Closure
var inst = new ClassB('foo');
var result = inst.publicMethod1('foo')
result = inst.publicMethod2('foo');
result = inst.publicMethod3('foo');
result = inst.publicMethod4('foo');
ready
Prototype + Internal call
var inst = new ClassC('foo');
var result = inst.publicMethod1('foo')
result = inst.publicMethod2('foo');
result = inst.publicMethod3('foo');
result = inst.publicMethod4('foo');
ready
Prototype + Internal self argument
var inst = new ClassD('foo');
var result = inst.publicMethod1('foo')
result = inst.publicMethod2('foo');
result = inst.publicMethod3('foo');
result = inst.publicMethod4('foo');
ready
Prototype + Internal self state
var inst = new ClassE('foo');
var result = inst.publicMethod1('foo')
result = inst.publicMethod2('foo');
result = inst.publicMethod3('foo');
result = inst.publicMethod4('foo');
ready

Revisions

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

  • Revision 1: published by Sebastian Markbåge on