Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hughsk/object-pool
Recycle objects with minimal boilerplate with an object pool
https://github.com/hughsk/object-pool
Last synced: 12 days ago
JSON representation
Recycle objects with minimal boilerplate with an object pool
- Host: GitHub
- URL: https://github.com/hughsk/object-pool
- Owner: hughsk
- License: other
- Created: 2013-08-11T05:38:55.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2017-04-29T18:08:57.000Z (over 7 years ago)
- Last Synced: 2024-10-17T16:41:12.895Z (22 days ago)
- Language: JavaScript
- Size: 243 KB
- Stars: 20
- Watchers: 5
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# object-pool #
A generic object pool which you can use for reducing object allocations when
you're creating and removing lots of objects in quick succession. Or just to
recycle objects, which is handy in cases such as game development.## Installation ##
``` javascript
npm install object-pool
```## Usage ##
### `pool = require('object-pool')([options])` ###
Creates a new object pool, taking the following arguments:
* `init`: a factory method which should return a freshly created object.
* `initSize`: a number to specify the initial size of reserved objects in the pool
* `enable`: called on an object when it's added to the pool.
* `disable`: called on an object when it's being removed from the pool.
* `key`: this module stores a reference to each node on each object. Use
this option to change the key it uses. Defaults to `__pool_node__`.### `pool.create()` ###
Returns a fresh object from the pool. If there aren't any objects left in the
reserve, this will call `init` to create a new object and then `enable` on
the object to get it set up. Otherwise, this will retrieve an object from the
reserve and just call `enable` on it.### `pool.remove(object)` ###
Removes an object from the pool, adding it to a reserve list to use later and
calling `disable` on it.### `pool.clean()` ###
Empties the "reserve" list of leftover objects.