UNPKG

sussy-util

Version:
31 lines (30 loc) 1.02 kB
export default class ObjectPool<T> { private readonly createObjectCallback; private pool; /** * Creates an object pool. * @param {() => T} createObjectCallback A function that creates new objects for the pool. * @param {number} [initialPoolSize=0] The initial size of the object pool. */ constructor(createObjectCallback: () => T, initialPoolSize?: number); /** * Populates the object pool with a specified number of objects. * @private * @param {number} count The number of objects to populate. */ private populatePool; /** * Acquires an object from the pool. If the pool is empty, a new object is created. * @returns {T} An object from the pool or a newly created object. */ acquire(): T; /** * Releases an object back to the pool for reuse. * @param {T} item The object to be released. */ release(item: T): void; /** * Clears the object pool, removing all objects. */ clear(): void; }