package/package.json000644 000765 000024 0000001057 13040006014013004 0ustar00000000 000000 { "name": "yapool", "version": "1.0.0", "description": "Yet Another object pool in JavaScript", "main": "index.js", "scripts": { "test": "tap test.js --100" }, "repository": { "type": "git", "url": "git+https://github.com/isaacs/yapool.git" }, "keywords": [], "author": "Isaac Z. Schlueter (http://blog.izs.me/)", "license": "ISC", "bugs": { "url": "https://github.com/isaacs/yapool/issues" }, "homepage": "https://github.com/isaacs/yapool#readme", "devDependencies": { "tap": "^9.0.3 || 10" } } package/.npmignore000644 000765 000024 0000000031 13040006046012511 0ustar00000000 000000 .nyc_output node_modules package/README.md000644 000765 000024 0000001066 13040004623012001 0ustar00000000 000000 # yapool Yet Another object pool in JavaScript Because [yallist](http://npm.im/yallist) is sometimes too featureful, this is a very dead-simple linked-list pool thingie in JavaScript that lets you add and remove objects in a set. Not suitable for very long lists, because all searches are `O(n)`, but for small `n`, it has very low complexity. ## API `p = new Pool()` Constructor takes no arguments `p.add(someObject)` put an object in the pool `p.length` return the number of things in the pool. `p.remove(someObject)` remove that object from the pool. package/LICENSE000644 000765 000024 0000001375 13040005047011533 0ustar00000000 000000 The ISC License Copyright (c) Isaac Z. Schlueter and Contributors Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. package/index.js000644 000765 000024 0000001670 13040005465012175 0ustar00000000 000000 module.exports = Pool function Pool () { this.length = 0 this.head = null this.tail = null } Pool.prototype.add = function (data) { this.tail = new Item(data, this.tail, null) if (!this.head) this.head = this.tail this.length ++ } Pool.prototype.remove = function (data) { if (this.length === 0) return var i = this.head.find(data) if (!i) return if (i === this.head) this.head = this.head.next if (i === this.tail) this.tail = this.tail.prev i.remove() this.length -- } function Item (data, prev) { this.prev = prev if (prev) prev.next = this this.next = null this.data = data } Item.prototype.remove = function () { if (this.next) this.next.prev = this.prev if (this.prev) this.prev.next = this.next this.prev = this.next = this.data = null } Item.prototype.find = function (data) { return data === this.data ? this : this.next ? this.next.find(data) : null } package/test.js000644 000765 000024 0000001706 13040005754012046 0ustar00000000 000000 var assert = require('assert') var P = require('./index.js') var a = { a: 1 } var b = { b: 1 } var t = require('tap') t.jobs = 64 process.env.TAP_BUFFER = 1 t.test(function removeFirstItem (t) { var p = new P p.add(a) p.add(b) p.remove(a) t.equal(p.length, 1) t.equal(p.head, p.tail) t.equal(p.head.data, b) t.end() }) t.test(function removeTail (t) { var p = new P p.add(a) p.add(b) p.remove(b) t.equal(p.length, 1) t.equal(p.head, p.tail) t.equal(p.head.data, a) t.end() }) t.test(function removeAll (t) { var p = new P p.add(a) p.add(b) p.remove(a) p.remove(b) t.equal(p.length, 0) t.equal(p.head, p.tail) t.equal(p.head, null) t.end() }) t.test(function removeExtra (t) { var p = new P p.add(a) p.add(b) p.remove(b) p.remove({some: 'thing not in there'}) p.remove(a) p.remove(a) p.remove(a) p.remove(a) t.equal(p.length, 0) t.equal(p.head, p.tail) t.equal(p.head, null) t.end() })