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 = ; { this;} SomeViewprototype { // do something} SomeViewprototype { // reset options on the view} SomeViewprototype { SomeViewpool;} SomeViewpool = ; moduleexports = SomeView;
Next, use it:
var SomeView = ; // create the viewvar view = SomeViewpool; // do stuffview; // we're done with the view, dispose itview; // call "create" again - it's the same view.console; // true
API
pool poolparty(options)
max
- the max number of objects allowed in a poolmin
- minimum number of items to use in the poolkeepAliveTimeout
- the number of MS before destroying a stale objectfactory
- the function which creates a pool objectrecycle
- called whenever an object is recycled
pool.size()
returns the size of the pool
pool.drip()
removes one item from the pool
pool.drain()
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
object.dispose()
disposes the object, and adds it back to the object pool