Simple JavaScript Object Pool
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Simple JavaScript Object Pool offered as both a MooTools class and a simple function constructor. Usage is pretty much the same for both implementations.

Object Pool

An object pool is a set of initialised objects that are kept ready to use, rather than allocated and destroyed on demand. A client of the pool will request an object from the pool and perform operations on the returned object. When the client has finished with an object, it returns it to the pool, rather than destroying it.


Create the objects that you want to reuse, store them in an array and pass them to the Pool constructor.

var objs = [new Request(), new Request()];
var request_pool = new Pool(objs);

Request an action to be taken against the next available object in the pool via the act method. Pool.act() takes a function whose signature is one of the pooled objects and the pool itself.

In the action you simply use the object and put it back into the pool by calling the Pool.add() method.

request_pool.act(function(request, pool){
    //use the object when it is available
        'url': '/some/long/request',
        'method': 'post'
        .addEvent('complete', function(){
            // put the object back when you're done with it


Four Fx.Morph instances are created to animate 400 elements.