poolparty

poolparty.js was created to reduce the overhead of creating expensive objects such as views, dom elements, etc. It's used in paperclip.js, and mojo.js.

Object Pooling & Recycling

poolparty.js was created to reduce the overhead of creating expensive objects such as views, dom elements, etc. It's used in paperclip.js, and mojo.js.

First create an object that you want to add to the pool party:

 
var poolParty = require("poolparty");
 
function SomeView (options) {
  this.reset(options);
}
 
 
SomeView.prototype.render = function () {
  // do something 
}
 
SomeView.prototype.reset = function (options) {
  // reset options on the view 
}
 
SomeView.prototype.dispose = function () {
  SomeView.pool.add(this);
}
 
 
SomeView.pool = poolParty({
  createfunction (options) {
    return new SomeView(options);
  },
  recyclefunction (someViewoptions) {
    someView.reset(options);
  }
});
 
 
module.exports = SomeView;

Next, use it:

var SomeView = require("./someView");
 
// create the view 
var view = SomeView.pool.create({});
 
// do stuff 
view.render();
 
// we're done with the view, dispose it 
view.dispose();
 
// call "create" again - it's the same view. 
console.log(SomeView.pool.create({}) === view); // true 
  • max - the max number of objects allowed in a pool
  • min - minimum number of items to use in the pool
  • keepAliveTimeout - the number of MS before destroying a stale object
  • factory - the function which creates a pool object
  • recycle - called whenever an object is recycled

returns the size of the pool

removes one item from the pool

removes all items from the pool except the min

Adds an object to the pool

creates, or uses a recycled object

disposes the object, and adds it back to the object pool