├── LICENSE.md ├── README.md ├── index.js └── package.json /LICENSE.md: -------------------------------------------------------------------------------- 1 | This software is released under the MIT license: 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | this software and associated documentation files (the "Software"), to deal in 5 | the Software without restriction, including without limitation the rights to 6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | the Software, and to permit persons to whom the Software is furnished to do so, 8 | subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 15 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 16 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 17 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # object-pool # 2 | 3 | A generic object pool which you can use for reducing object allocations when 4 | you're creating and removing lots of objects in quick succession. Or just to 5 | recycle objects, which is handy in cases such as game development. 6 | 7 | ## Installation ## 8 | 9 | ``` javascript 10 | npm install object-pool 11 | ``` 12 | 13 | ## Usage ## 14 | 15 | ### `pool = require('object-pool')([options])` ### 16 | 17 | Creates a new object pool, taking the following arguments: 18 | 19 | * `init`: a factory method which should return a freshly created object. 20 | * `initSize`: a number to specify the initial size of reserved objects in the pool 21 | * `enable`: called on an object when it's added to the pool. 22 | * `disable`: called on an object when it's being removed from the pool. 23 | * `key`: this module stores a reference to each node on each object. Use 24 | this option to change the key it uses. Defaults to `__pool_node__`. 25 | 26 | ### `pool.create()` ### 27 | 28 | Returns a fresh object from the pool. If there aren't any objects left in the 29 | reserve, this will call `init` to create a new object and then `enable` on 30 | the object to get it set up. Otherwise, this will retrieve an object from the 31 | reserve and just call `enable` on it. 32 | 33 | ### `pool.remove(object)` ### 34 | 35 | Removes an object from the pool, adding it to a reserve list to use later and 36 | calling `disable` on it. 37 | 38 | ### `pool.clean()` ### 39 | 40 | Empties the "reserve" list of leftover objects. 41 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var LinkedList = require('circular-list') 2 | var Node = LinkedList.Node 3 | 4 | module.exports = Pool 5 | 6 | function noop(){} 7 | function defaultInit() { 8 | return {} 9 | } 10 | 11 | function Pool(opts) { 12 | if (!(this instanceof Pool)) { 13 | return new Pool(opts) 14 | } 15 | 16 | opts = opts || {} 17 | this.reserve = new LinkedList 18 | this.list = new LinkedList 19 | 20 | this.key = opts.key || '__pool_node__' 21 | this.disable = opts.disable || noop 22 | this.enable = opts.enable || noop 23 | this.init = opts.init || defaultInit 24 | this.initPool(opts.initSize || 0); 25 | } 26 | 27 | Pool.prototype.initPool = function(initialSize) { 28 | for(var i = 0; i < initialSize; i++) { 29 | var object = this.init() 30 | var node = new Node(object) 31 | object[this.key] = node 32 | this.reserve.append(object[this.key]) 33 | } 34 | } 35 | 36 | Pool.prototype.create = function() { 37 | if (this.reserve.first === null) { 38 | var object = this.init() 39 | var node = new Node(object) 40 | object[this.key] = node 41 | } else { 42 | var node = this.reserve.first 43 | var object = node.data 44 | this.reserve.remove(node) 45 | } 46 | 47 | this.enable(object) 48 | this.list.append(node) 49 | 50 | return object 51 | } 52 | 53 | Pool.prototype.remove = function(entity) { 54 | this.disable(entity) 55 | this.list.remove(entity[this.key]) 56 | this.reserve.append(entity[this.key]) 57 | return this 58 | } 59 | 60 | Pool.prototype.each = function(cb) { 61 | var p = this.list.first 62 | var n = this.list.length 63 | 64 | while (n--) { 65 | cb(p.data) 66 | p = p.next 67 | } 68 | } 69 | 70 | Pool.prototype.clean = function() { 71 | this.reserve.length = 0 72 | this.reserve.first = null 73 | this.reserve.last = null 74 | } 75 | 76 | var pool = Pool(function init() { 77 | return { active: false } 78 | }, { 79 | key: '__pool_node__', 80 | enable: function(entity) { 81 | entity.active = true 82 | }, 83 | disable: function(entity) { 84 | entity.active = false 85 | } 86 | }) 87 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "object-pool", 3 | "version": "0.2.0", 4 | "description": "An object pool for recycling objects without as much boilerplate", 5 | "main": "index.js", 6 | "scripts": {}, 7 | "repository": { 8 | "type": "git", 9 | "url": "git://github.com/hughsk/object-pool.git" 10 | }, 11 | "keywords": [ 12 | "object", 13 | "pool", 14 | "recycle", 15 | "reuse" 16 | ], 17 | "author": "Hugh Kennedy (http://hughskennedy.com/)", 18 | "license": "MIT", 19 | "readmeFilename": "README.md", 20 | "dependencies": { 21 | "circular-list": "0.0.0" 22 | } 23 | } 24 | --------------------------------------------------------------------------------