A pool of reusable objects that can be used to avoid allocation.

// Create a pool and preallocate it with 10 ExpensiveObjects.
var pool = new Pool(function () return new ExpensiveObject()).setSize(10);

var obj = pool.take();
// ...
pool.put(obj);

Constructor

View source

new (allocator :Void -> A)

Parameters:

allocator

A function that creates a new object.

Methods

View source

put (object :A) :Void

Put an object into the pool. This should be called to release objects previously claimed with take(). Can also be called to pre-allocate the pool with new objects.

View source

setCapacity (capacity :Int) :Pool<A>

Sets the maximum capacity of the pool. By default, the pool can grow to any size.

Returns:

This instance, for chaining.

View source

setSize (size :Int) :Pool<A>

Resizes the pool. If the given size is larger than the current number of pooled objects, new objects are allocated to expand the pool. Otherwise, objects are trimmed out of the pool.

Returns:

This instance, for chaining.

View source

take () :A

Take an object from the pool. If the pool is empty, a new object will be allocated.

You should later release the object back into the pool by calling put().