Classes with Many Inner Function Calls

Benchmark created by Sebastian Markbåge on


Preparation HTML

<script>
  var ClassA = function(state) {
   this.state = state;
  };
  ClassA.prototype = {
  
   publicMethod1: function(x) {
    for (var i = 0; i < 1000; i++)
    if (this.publicMethod2(x, i)) return true;
    return false;
   },
  
   publicMethod2: function(x, i) {
    return this.state == x + i;
   }
  
  };
  
  var ClassB = function(state) {
  
   function privateMethod1(x) {
    for (var i = 0; i < 1000; i++)
    if (privateMethod2(x, i)) return true;
    return false;
   };
  
   function privateMethod2(x, i) {
    return state == x + i;
   };
  
   this.publicMethod1 = privateMethod1;
   this.publicMethod2 = privateMethod2;
  
  };
  
  var ClassC = function(state) {
   this.state = state;
  };
  ClassC.prototype = (function() {
  
   function privateMethod1(x) {
    for (var i = 0; i < 1000; i++)
    if (privateMethod2.call(this, x, i)) return true;
    return false;
   };
  
   function privateMethod2(x, i) {
    return this.state == x + i;
   };
  
   return {
  
    publicMethod1: privateMethod1,
    publicMethod2: privateMethod2
  
   };
  
  })();
  
  
  var ClassD = function(state) {
   this.state = state;
  };
  ClassD.prototype = (function() {
  
   function privateMethod1(self, x) {
    for (var i = 0; i < 1000; i++)
    if (privateMethod2(self, x, i)) return true;
    return false;
   };
  
   function privateMethod2(self, x, i) {
    return self.state == x + i;
   };
  
   return {
  
    publicMethod1: function(x) {
     return privateMethod1(this, x);
    },
  
    publicMethod2: function(x, i) {
     return privateMethod2(this, x, i);
    }
  
   };
  
  })();
  
  var ClassE = function(state) {
   this.state = state;
  };
  
  ClassE.prototype = (function() {
  
   var state;
  
   function privateMethod1(x) {
    for (var i = 0; i < 1000; i++)
    if (privateMethod2(x, i)) return true;
    return false;
   };
  
   function privateMethod2(x, i) {
    return state == x + i;
   };
  
   return {
  
    publicMethod1: function(x) {
     var stack = state;
     state = this.state;
     var result = privateMethod1(x);
     state = stack;
     return result;
    },
  
    publicMethod2: function(x, i) {
     var stack = state;
     state = this.state;
     var result = privateMethod2(x, i);
     state = stack;
     return result;
    }
  
   };
  
  })();
  
  var instA = new ClassA(10);
  var instB = new ClassB(10);
  var instC = new ClassC(10);
  var instD = new ClassD(10);
  var instE = new ClassE(10);
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Plain prototype
var result = instA.publicMethod1(100);
ready
Closure
var result = instB.publicMethod1(100);
ready
Prototype + Internal call
var result = instC.publicMethod1(100);
ready
Prototype + Internal self argument
var result = instD.publicMethod1(100);
ready
Prototype + Internal self state
var result = instE.publicMethod1(100);
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