A tiny TypeScript object pool
npm install nano-poolAllows 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
---
``ts
import { createObjectPool } from 'nano-pool'
const poolSize = 10
const createObject = () => {
const sprite = new Sprite()
sprite.scale.set(4)
return sprite
}
const objectPool = createObjectPool(poolSize, createObject)
const object1 = objectPool.get()
objectPool.release(object1)
objectPool.releaseAll()
`
---
`console`
npm install nano-pool
---
`ts`
createObjectPool
`ts`
type ObjectPool
get: () => T
release: (object: T) => void
releaseAll: () => void
countAll: () => number
}
`ts`
type Options
/* 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
---
#### There are no more objects in the pool when take is called
In development: an error will be thrown.
In production: a new object is created and added to the pool.
#### An object already in the pool is released
In development: an error will be thrown.
In production`: no-op.