Allows you to pre-instantiate objects and then request them from a pool
- Tiny bundle size
- Written in TypeScript
- Zero dependencies
- Adheres to the Unity game engine's naming conventions
import { createObjectPool } from 'nano-pool'
const poolSize = 10
const createObject = () => new Sprite()
const objectPool = createObjectPool(poolSize, createObject, {
onRelease: (sprite) => {
sprite.scale.set(4)
},
})
const object1 = objectPool.get()
objectPool.release(object1)
objectPool.releaseAll()
npm install nano-pool
createObjectPool<T>(size: number, createObject: (index: number) => T, options?: Options): ObjectPool
type ObjectPool<T> = {
get: () => T
release: (object: T) => void
releaseAll: () => void
countAll: () => number
}
type Options<T> = {
/** Improves debug output */
id?: string
/** Called when object is released back into the pool
*
* Note: This is also called when the object is created, so that any reset logic can be shared
*/
onRelease?: (object: T) => void
}
The amount of objects to create
The function that will be used to create new objects
In development
: an error will be thrown.
In production
: a new object is created and added to the pool.
In development
: an error will be thrown.
In production
: no-op.