Classes with Many Inner Function Calls

JavaScript performance comparison

Test case created by Sebastian Markbåge

Preparation code

<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

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Plain prototype
var result = instA.publicMethod1(100);
pending…
Closure
var result = instB.publicMethod1(100);
pending…
Prototype + Internal call
var result = instC.publicMethod1(100);
pending…
Prototype + Internal self argument
var result = instD.publicMethod1(100);
pending…
Prototype + Internal self state
var result = instE.publicMethod1(100);
pending…

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

Compare results of other browsers

0 Comments