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 |
|
4 | This package implements a custom-tailored object pool for PixiJS applications. It provides the
|
5 | following 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
|
12 | per-frame.
|
13 |
|
14 | This package can also be used as a _single-source_ of object pools. If two different libraries need
|
15 | a 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
|
20 | is 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
|
25 | limit of objects you need per frame.
|
26 |
|
27 | ## Installation :package:
|
28 |
|
29 | ```bash
|
30 | npm install @pixi-essentials/object-pool
|
31 | ```
|
32 |
|
33 | ## Usage :page_facing_up:
|
34 |
|
35 | ```ts
|
36 | import { ObjectPoolFactory } from '@pixi-essentials/object-pool';
|
37 | import { Rectangle } from '@pixi/math';
|
38 |
|
39 | const rpool: ObjectPoolFactory = ObjectPoolFactory.build(Rectangle);
|
40 |
|
41 | rpool.reserve(10000);
|
42 | rpool.startGC();// prevent pool from staying above 10,000 rectangles for too long
|
43 |
|
44 | const rect: PIXI.Rectangle = rpool.allocate();
|
45 |
|
46 | // do something
|
47 |
|
48 | rpool.release(rect);
|
49 |
|
50 | // Want to reduce pool size now?
|
51 | rpool.limit(11000);
|
52 | ```
|