Simple object pool implementation.
$ npm install --save @vaalentin/object-pool
import ObjectPool from '@vaalentin/object-pool';
class Vec2 {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
const pool = new ObjectPool({
create: (x, y) => new Vec2(x, y),
reset: (vec, x, y) => {
vec.x = x;
vec.y = y;
return vec;
}
});
const vec = pool.get(1, 3);
pool.free(vec);
Create a new pool, with the possible following opts
:
Property | Type | Description | Default |
---|---|---|---|
maxSize | int |
max pool size | 0 |
collectFreq | float |
how often to remove excess objects (in ms) | -1 |
create | () => T |
function to create object | () => {} |
reset | (T) => T |
function to reset object | obj => obj |
Get an object from the pool by calling the reset
options with the given args
.
Release an object when not needed anymore.
Remove availabe objects excess.
MIT, see LICENSE.md for more details.