UNPKG

1.55 kBMarkdownView Raw
1# Object Pool
2[![install size](https://packagephobia.now.sh/badge?p=@pixi-essentials/object-pool)](https://packagephobia.now.sh/result?p=@pixi-essentials/object-pool)
3
4This package implements a custom-tailored object pool for PixiJS applications. It provides the
5following features:
6
7* **reserve**: You can preallocate the pool size to have a set amount of objects.
8
9* **limit**: You can reduce the pool size after a lot of allocations.
10
11* **auto-GC**: The GC will reduce your pool to the reserve size after allocation demand goes down
12per-frame.
13
14This package is can also be used as a _single-source_ of object pools. If two different libraries need
15a pool for say, `PIXI.Rectangle`, then the same object pool will be returned.
16
17### Analysis
18
19* https://codepen.io/sukantpal/pen/zYvBOVw: This chart shows the pool capacity and the allocations done per frame. The GC
20is enabled and reserve is set to 100,000.
21
22<img src="https://i.ibb.co/jkNWHdR/Screen-Shot-2020-04-18-at-12-46-08-PM.png"></img>
23
24* You should use auto-GC only if allocations-per-frame is smooth (slowly increase & slowly decrease) or you know the upper
25limit of objects you need per frame.
26
27## Usage
28
29```ts
30import { ObjectPoolFactory } from 'pixi-object-pool';
31
32const rpool: ObjectPoolFactory = ObjectPoolFactory.build(PIXI.Rectangle);
33
34rpool.reserve(10000);
35rpool.startGC();// prevent pool from staying above 10,000 rectangles for too long
36
37const rect: PIXI.Rectangle = rpool.allocate();
38
39// do something
40
41rpool.release(rect);
42
43// Want to reduce pool size now?
44rpool.limit(11000);
45```