Created
September 4, 2016 04:32
-
-
Save mattoni/2b3ec9c93b280ef995d7e7403856f755 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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