UNPKG

1.71 kBMarkdownView Raw
1# @pixi-essentials/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 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## Installation :package:
28
29```bash
30npm install @pixi-essentials/object-pool
31```
32
33## Usage :page_facing_up:
34
35```ts
36import { ObjectPoolFactory } from '@pixi-essentials/object-pool';
37import { Rectangle } from '@pixi/math';
38
39const rpool: ObjectPoolFactory = ObjectPoolFactory.build(Rectangle);
40
41rpool.reserve(10000);
42rpool.startGC();// prevent pool from staying above 10,000 rectangles for too long
43
44const rect: PIXI.Rectangle = rpool.allocate();
45
46// do something
47
48rpool.release(rect);
49
50// Want to reduce pool size now?
51rpool.limit(11000);
52```