Pooling test

JavaScript performance comparison

Revision 6 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var aliveGuysCount = 100;
  var allGuysCount   = 50000;
  var currentGuys = [];
  
  var randInt=function(max) {
     return Math.floor(max * Math.random());
  }
  
  var setupPool  =  function(func, initialPoolSize) {
      if (arguments.length<2) { 
          throw ('setupPool takes two arguments');    }
      func.pool                = []        ;
      func.poolSize            = 0         ;
      func.maxPoolSize         = initialPoolSize ;
      func.pnew                = pnew      ;
      func.prototype.pdispose  = pdispose  ; 
      // pre-fill the pool.
      for (var i=0; i<initialPoolSize; i++) {
          (new func()).pdispose();
       }
  };
  
  var pnew     = function() {
      var pnewObj  = null     ; 
      if (this.poolSize>0 ) {
             // the pool contains objects -> grab one
             this.poolSize--  ;
             pnewObj = this.pool[this.poolSize];
             this.pool[this.poolSize] = null   ; // **
      } else {
             // the pool is empty -> create new object
             pnewObj = new this() ;
      }
      // return initialized object.
      return this.apply(pnewObj, arguments);
  };
  
  var pdispose   = function() {
      var thisCttr = this.constructor             ;
      // throw the object back in the pool 
      thisCttr.pool[thisCttr.poolSize++] = this ;
      // update maxPoolSize
      thisCttr.maxPoolSize = Math.max(thisCttr.maxPoolSize,
                                               thisCttr.poolSize); 
  };
  
  var BadGuy = function(posX, posY, gun, ammo) {
        this.posX   = ( posX === undefined ) ? 54   : posX   ;  
        this.posY   = ( posY === undefined ) ? 0     : posY  ;
        this.speedX = 10    ;  this.speedY = 0               ;
        this.gun    = ( gun  === undefined ) ? 'AK47': gun   ;  
        this.ammo   = ( ammo === undefined ) ? 100   : ammo  ;
        this.alive  = true  ;
        this.dummy1=this.dummy2=this.dummy3=this.dummy4=this.dummy5=0;
        return this;
        };
  
  BadGuy.prototype.move = function(dt) {
        this.posX += this.speedX * dt;
        this.posY += this.speedY * dt;
  };
  
  BadGuy.prototype.canShoot = function() {       
         return (this.ammo != 0);
  };
  
  setupPool(BadGuy,aliveGuysCount);
  
  var BadGuy2 = function(posX, posY, gun, ammo) {
        this.posX   = ( posX === undefined ) ? 54   : posX   ;  
        this.posY   = ( posY === undefined ) ? 0     : posY  ;
        this.speedX = 10    ;  this.speedY = 0               ;
        this.gun    = ( gun  === undefined ) ? 'AK47': gun   ;  
        this.ammo   = ( ammo === undefined ) ? 100   : ammo  ;
        this.dummy1=this.dummy2=this.dummy3=this.dummy4=this.dummy5=0;
      this.dummy6=this.dummy7=this.dummy8=this.dummy9=this.dummy10=0;
        this.alive  = true  ;
        return this;
        };
  
  BadGuy2.prototype.move = function(dt) {
        this.posX += this.speedX * dt + 2;
        this.posY += this.speedY * dt + 2;
  };
  
  BadGuy2.prototype.canShoot = function() {       
         return (this.ammo +2 != 0);
  };
  
  setupPool(BadGuy2,aliveGuysCount);
  
  var BadGuy3 = function(posX, posY, gun, ammo) {
        this.posX   = ( posX === undefined ) ? 54   : posX   ;  
        this.posY   = ( posY === undefined ) ? 0     : posY  ;
        this.speedX = 10    ;  this.speedY = 0               ;
        this.gun    = ( gun  === undefined ) ? 'AK47': gun   ;  
        this.ammo   = ( ammo === undefined ) ? 100   : ammo  ;
        this.alive  = true  ;
        return this;
        };
  
  BadGuy3.prototype.move = function(dt) {
        this.posX += this.speedX * dt + 3;
        this.posY += this.speedY * dt + 3;
  };
  
  BadGuy3.prototype.canShoot = function() {       
         return (this.ammo + 3 != 0);
  };
  
  setupPool(BadGuy3,aliveGuysCount);
  
  var BadGuy4 = function(posX, posY, gun, ammo) {
        this.posX   = ( posX === undefined ) ? 54   : posX   ;  
        this.posY   = ( posY === undefined ) ? 0     : posY  ;
        this.speedX = 10    ;  this.speedY = 0               ;
        this.gun    = ( gun  === undefined ) ? 'AK47': gun   ;  
        this.ammo   = ( ammo === undefined ) ? 100   : ammo  ;
        this.dummy1=this.dummy2=this.dummy3=this.dummy4=this.dummy5=0;
      this.dummy6=this.dummy7=this.dummy8=this.dummy9=this.dummy10=0;
      this.dummy11=this.dummy12=this.dummy13=this.dummy14=this.dummy15=0;
        this.alive  = true  ;
        return this;
        };
  
  BadGuy4.prototype.move = function(dt) {
        this.posX += this.speedX * dt + 2;
        this.posY += this.speedY * dt + 2;
  };
  
  BadGuy4.prototype.canShoot = function() {       
         return (this.ammo +2 != 0);
  };
  
  setupPool(BadGuy4,aliveGuysCount);
  
  var objectsArray=[];
  objectsArray.push(BadGuy);
  objectsArray.push(BadGuy2);
  objectsArray.push(BadGuy3);
  objectsArray.push(BadGuy4);

};
</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
Create/Dispose : classical way
for (var i=0; i<allGuysCount; i++) {
  var newGuy= (objectsArray[randInt(4)]).pnew();
  currentGuys.push(newGuy);
  if (newGuy.canShoot()) {newGuy.move();}
  if (currentGuys.length>=aliveGuysCount) {
    currentGuys.shift();
   }
}
pending…
using pool, not pre-filled
BadGuy.poolSize=BadGuy2.poolSize=BadGuy3.poolSize=0;

for (var i=0; i<allGuysCount; i++) {
  var newGuy= (objectsArray[randInt(4)]).pnew();
  currentGuys.push(newGuy);
  if (newGuy.canShoot()) {newGuy.move();}
  if (currentGuys.length>=aliveGuysCount) {
    var killedGuy = currentGuys.shift();
    killedGuy.pdispose();
   }
}
pending…
using pool, pre-filled
for (var i=0; i<allGuysCount; i++) {
  var newGuy= (objectsArray[randInt(4)]).pnew();
  currentGuys.push(newGuy);
  if (newGuy.canShoot()) {newGuy.move();}
  if (currentGuys.length>=aliveGuysCount) {
    var killedGuy = currentGuys.shift();
    killedGuy.pdispose();
   }
}
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.

0 Comments