|
| 1 | +# deePool |
| 2 | + |
| 3 | +A highly-efficient simple (no frills) object pool. |
| 4 | + |
| 5 | +## Explanation |
| 6 | + |
| 7 | +**deePool** (aka "deep-pool") is an object pool with efficiency (speed, low memory, little-to-no GC) as its primary design goal. |
| 8 | + |
| 9 | +As such, there are no configuration options to tweak behavior. It does one thing, and one thing well. If you want to re-configure the behavior, modify the code. :) |
| 10 | + |
| 11 | +Also, this library doesn't really try to do much in the way of graceful handling of errors or mistakes, as that stuff just slows it down. You should be careful in how you use *deePool*. |
| 12 | + |
| 13 | +## Environment Support |
| 14 | + |
| 15 | +This library requires ES6+ support. If you need to use it in ES<=5, transpile it with Babel yourself. |
| 16 | + |
| 17 | +## Library API |
| 18 | + |
| 19 | +There's only one method on the API: |
| 20 | + |
| 21 | +```js |
| 22 | +deePool.create( objectFactory ) |
| 23 | +``` |
| 24 | + |
| 25 | +* `create(..)`: produces a new pool instance. You can create as many separate pools as you need. |
| 26 | + |
| 27 | + `objectFactory` must be a function that produces a single new empty object (or array, etc) that you want available in the pool. Examples: |
| 28 | + |
| 29 | + ```js |
| 30 | + var myArrays = deePool.create( function makeArray(){ |
| 31 | + return [ |
| 32 | + [ "foo", "bar", "baz" ], |
| 33 | + [ 1, 2, 3 ] |
| 34 | + ]; |
| 35 | + } ); |
| 36 | + |
| 37 | + var myWidgets = deePool.create( function makeWidgets(){ |
| 38 | + return new SomeCoolWidget(1,2,3); |
| 39 | + } ); |
| 40 | + ``` |
| 41 | + |
| 42 | +### Pool API |
| 43 | + |
| 44 | +Each pool has four simple methods on its API: |
| 45 | + |
| 46 | +```js |
| 47 | +pool.use() |
| 48 | + |
| 49 | +pool.recycle( obj ) |
| 50 | + |
| 51 | +pool.grow( additionalSize ) |
| 52 | + |
| 53 | +pool.size() |
| 54 | +``` |
| 55 | + |
| 56 | +* `pool.use()`: retrieves an available object instance from the pool. Example: |
| 57 | + |
| 58 | + ```js |
| 59 | + var arr = myArrays.use(); |
| 60 | + ``` |
| 61 | + |
| 62 | + **Note:** If the pool doesn't have any free instances, it will automatically grow (double in size, or set to `5` if currently empty) and then return one of the new instances. |
| 63 | + |
| 64 | +* `pool.recycle(..)`: inserts an object instance back into the pool. Example: |
| 65 | + |
| 66 | + ```js |
| 67 | + myArrays.recycle( arr ); |
| 68 | + ``` |
| 69 | + |
| 70 | + **Tips:** |
| 71 | + - Don't forget to `recycle(..)` object instances after you're done using them. They are not automatically recycled, and your pool will run out of available instances, and thus keep growing unboundedly if you don't recycle. |
| 72 | + - Don't `recycle(..)` an object instance until you're fully done with it. As objects are held by reference in JS, if you hold onto a reference and modify an already recycled object, you will cause difficult to track down bugs in your application! To avoid this pitfall, unset your reference(s) to a pooled object immediately after `recycle(..)`. |
| 73 | + - Don't `recycle(..)` objects that weren't created for the pool and extracted by a previous `use()`. |
| 74 | + - Don't `recycle(..)` an object more than once. This will end up creating multiple references in the pool to the same object, which will cause difficult to track down bugs in your application! To avoid this pitfall, unset your reference(s) to a pooled object immediately after `recycle(..)`. |
| 75 | + - Don't create references between object instances in the pool, or you will cause difficult to track down bugs in your application! |
| 76 | + - If you need to "condition" or "reset" an object instance for its later reuse, do so before passing it into `recycle(..)`. If a pooled object holds references to other objects, and you want that memory freed up, make sure to unset those references. |
| 77 | + |
| 78 | + **Note:** If you insert an object instance into a pool that has no empty slots (this is always a mistake, but is not disallowed!), the result will be that the pool grows in size by 1. |
| 79 | + |
| 80 | +* `pool.grow(..)`: A number passed will specify how many instances to add to the pool (created by `objectFactory()` as specified in the [`deePool.create(..)` call](#Library_API)). If no number is passed, the default is the current size of the pool (effectively doubling it in size). Examples: |
| 81 | + |
| 82 | + ```js |
| 83 | + var myPool = deePool.create(makeArray); |
| 84 | + |
| 85 | + var arr = myPool.use(); // pool size now `5` |
| 86 | + |
| 87 | + myPool.grow( 3 ); // pool size now `8` |
| 88 | + myPool.grow(); // pool size now `16` |
| 89 | + ``` |
| 90 | + |
| 91 | + **Tips:** |
| 92 | + - A new pool starts out empty (size: `0`). Always call `grow(..)` with a valid positive (non-zero) number to initialize the pool before using it. Otherwise, the call will have no effect; on an empty pool, this will confusingly leave the pool empty. |
| 93 | + - An appropriate initial size for the pool will depend on the tasks you are performing; essentially, how many objects will you need to use concurrently? |
| 94 | + |
| 95 | + You should profile for this with your use-case(s) to determine what the most likely maximum pool size is. You'll want a pool size that's big enough so it doesn't have to grow very often, but not so big that it wastes memory. |
| 96 | + - Don't grow the pool manually unless you are really sure you know what you're doing. It's better to set the pool size initially and let it grow automatically (via `use()`) only as it needs to. |
| 97 | + |
| 98 | +* `pool.size()`: Returns the number of overall slots in the pool (both used and unused). Example: |
| 99 | + |
| 100 | + ```js |
| 101 | + var myPool = deePool.create(makeArray); |
| 102 | + |
| 103 | + myPool.size(); // 0 |
| 104 | + |
| 105 | + myPool.grow(5); |
| 106 | + myPool.grow(10); |
| 107 | + myPool.grow(5); |
| 108 | + |
| 109 | + myPool.size(); // 20 |
| 110 | + ``` |
| 111 | + |
| 112 | +## Builds |
| 113 | + |
| 114 | +The core library file can be built (minified) with an included utility: |
| 115 | + |
| 116 | +``` |
| 117 | +./build-core.js |
| 118 | +``` |
| 119 | + |
| 120 | +However, the recommended way to invoke this utility is via npm: |
| 121 | + |
| 122 | +``` |
| 123 | +npm run-script build-core |
| 124 | +``` |
| 125 | + |
| 126 | +## Tests |
| 127 | + |
| 128 | +To run the tests, you must first [build the core library](#Builds). |
| 129 | + |
| 130 | +With `npm`, run: |
| 131 | + |
| 132 | +``` |
| 133 | +npm test |
| 134 | +``` |
| 135 | + |
| 136 | +Or, manually: |
| 137 | + |
| 138 | +``` |
| 139 | +node node-tests.js |
| 140 | +``` |
| 141 | + |
| 142 | +## License |
| 143 | + |
| 144 | +The code and all the documentation are released under the MIT license. |
| 145 | + |
| 146 | +http://getify.mit-license.org/ |
0 commit comments