Skip to content

Instantly share code, notes, and snippets.

@mattoni
Created September 4, 2016 04:32
Show Gist options
  • Save mattoni/2b3ec9c93b280ef995d7e7403856f755 to your computer and use it in GitHub Desktop.
Save mattoni/2b3ec9c93b280ef995d7e7403856f755 to your computer and use it in GitHub Desktop.
export default class ObjectPool<T> {
private metrics = {
allocated: 0,
free: 0
};
private pool: T[] = [];
constructor(private type: new () => T) { }
public alloc(): T {
let obj = this.pool.pop();
if (obj) {
this.metrics.free--;
return obj;
}
this.metrics.allocated++;
return new this.type();
}
public free(obj: T) {
this.pool.push(obj);
this.metrics.free++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment