UNPKG

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