fast-pool, A lightweight and fast object pool implementation
About
fast-pool is yet another object pooling for JavaScript.
Q: OMG, why another?
A: Because the existing ones didn't meet my demands.
Features
Fast: fastPool doesn't do fancy magic. It just pools objects. And your object is only one function call away from you when you need it.
Configurable: fastPool doesn't only pool plain constructors. It allows you to pass a function that returns your object, whatever it is.
Clean: fastPool offers you to clean up dirty objects before putting them back into the pool
Usage
Either directly download fast-pool.min.js, or get it via package managers:
NPM:
$ npm install fast-pool
Bower:
$ bower install fast-pool
fastPool comes in UMD format, so you can either include it in a script tag, require it as an AMD module or require it as a CommonJS module.
Once required, it will expose a function (called fastPool
if linked vie script tag), which has the following signature:
var pool = fastPool(options);
options
is an object containing the following properties:
- name (string): The name that identifies this pool
- ctor (function, optional): A constructor, that, when called with the new keyword, returns a new object
- createObject (function, optional): A function that returns a new object
- preAllocate (number, optional, default 0): The number of objects to pre-allocate
- resetObject (function, optional): A function that will turn a used, 'dirty' object into a clean state, will get the used object as first and only argument
You MUST pass either the ctor
or the reateObject
property.
Examples
var vec3Pool = fastPool({
name: 'vec3',
ctor: Vector3,
preAllocate: 20
});
var vec3 = vec3Pool.transfer();
vec3Pool.takeBack(vec3);
var vec3Pool = fastPool({
name: 'vec3',
ctor: Vector3,
preAllocate: 20,
resetObject: function (vec3) {
vec3.set(0, 0, 0);
}
});
var vec3 = vec3Pool.transfer();
vec3.x = 5;
vec3Pool.takeBack(vec3);
var matrix16Pool = fastPool({
name: 'matrix4',
preAllocate: 100,
createObject: function () {
return new Float32Array(16);
}
});
function createTieFighter () {
var tieFighter = new Fighter({
type: 'TieFighter',
AIStrength: 3
});
tieFighter.setTarget(player);
return tieFighter;
}
function resetTieFighter (tieFighter) {
tieFighter.clearTarget();
tieFighter.resetPosition();
}
var tieFighterPool = fastPool({
name: 'TieFighter',
preAllocate: 5,
createObject: createTieFighter,
resetObject: resetTieFighter
});
Reference
A reference is over here: http://jensarps.github.io/fast-pool/reference/global.html#fastPool
License
MIT