node package manager
Love JavaScript? Your insights can make it even better. Take the 2017 JavaScript Ecosystem Survey »


Object Pooling & Recycling Alt ci

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) {
SomeView.prototype.render = function () {
  // do something 
SomeView.prototype.reset = function (options) {
  // reset options on the view 
SomeView.prototype.dispose = function () {
SomeView.pool = poolParty({
  create: function (options) {
    return new SomeView(options);
  recycle: function (someView, options) {
module.exports = SomeView;

Next, use it:

var SomeView = require("./someView");
// create the view 
var view = SomeView.pool.create({});
// do stuff 
// we're done with the view, dispose it 
// call "create" again - it's the same view. 
console.log(SomeView.pool.create({}) === view); // true 


pool poolparty(options)

  • 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

object pool.add(item)

Adds an object to the pool

object pool.create(options)

creates, or uses a recycled object


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