├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 'node' 4 | - '8' 5 | - '6' 6 | - '4' 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 Mathias Buus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # buffer-reuse-pool 2 | 3 | An easy way to reuse buffers without much API ceremony 4 | 5 | ``` 6 | npm install buffer-reuse-pool 7 | ``` 8 | 9 | [![build status](https://travis-ci.org/mafintosh/buffer-reuse-pool.svg?branch=master)](https://travis-ci.org/mafintosh/buffer-reuse-pool) 10 | 11 | ## Usage 12 | 13 | ``` js 14 | const reuse = require('buffer-reuse-pool') 15 | const pool = reuse.pool(65536) // make a pool of 64kb chunks 16 | 17 | const buf = pool.alloc() // alloc a buffer 18 | 19 | // ... do stuff like passing it to fs.read 20 | // when done free it 21 | 22 | reuse.free(buf) // re-adds it to the pool it was allocated from 23 | ``` 24 | 25 | The neat thing is that the `buf` variable is just a normal buffer and that the 26 | reuse function does not need to know about the pool, meaning that module authors 27 | can support buffer reuse without any additional api complexities. 28 | 29 | All you have to do is free a buffer when you are completely done using it 30 | 31 | ## API 32 | 33 | #### `const pool = reuse.pool(size)` 34 | 35 | Make a new buffer reuse pool. `size` if the size of the buffers allocated. 36 | 37 | #### `pool.size` 38 | 39 | Get the buffer size of this pool. 40 | 41 | #### `const buffer = pool.alloc()` 42 | 43 | Allocate a new buffer of size `pool.size`. 44 | Note that the buffer is *not* guaranteed to be blank as it can have been used before. 45 | 46 | #### `const reusable = reuse.free(buffer)` 47 | 48 | Free a buffer. Returns `true` if this buffer was reuseable, `false` if not. 49 | It is safe to pass a normal buffer allocated with `Buffer.alloc` or a one allocated with `pool.alloc`. 50 | 51 | #### `const slicedBuffer = reuse.slice(buffer, start, end)` 52 | 53 | Slice a buffer. If the `slicedBuffer` is freed the original `buffer` is reused. 54 | 55 | ## License 56 | 57 | MIT 58 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const BUFFER_POOL = createSymbol('BufferPool') 2 | const BUFFER_SLICE = createSymbol('BufferSlice') 3 | 4 | function BufferPool (size) { 5 | this.free = [] 6 | this.size = size 7 | } 8 | 9 | BufferPool.prototype.alloc = function () { 10 | return this.free.pop() || this._allocNew() 11 | } 12 | 13 | BufferPool.prototype._allocNew = function () { 14 | const buf = Buffer.alloc(this.size) 15 | buf[BUFFER_POOL] = this 16 | return buf 17 | } 18 | 19 | // Trying to help v8. TODO: Is this needed? 20 | Buffer.prototype[BUFFER_POOL] = null 21 | Buffer.prototype[BUFFER_SLICE] = null 22 | 23 | exports.slice = function (buf, start, end) { 24 | const slice = buf.slice(start, end) 25 | slice[BUFFER_SLICE] = buf[BUFFER_SLICE] || buf 26 | slice[BUFFER_POOL] = buf[BUFFER_POOL] 27 | return slice 28 | } 29 | 30 | exports.pool = function (size) { 31 | return new BufferPool(size) 32 | } 33 | 34 | exports.free = function (buf) { 35 | const pool = buf[BUFFER_POOL] 36 | if (!pool) return false 37 | pool.free.push(buf[BUFFER_SLICE] || buf) 38 | return true 39 | } 40 | 41 | function createSymbol (name) { 42 | if (typeof Symbol !== 'undefined') return Symbol(name) 43 | return '__SYMBOL__' + Date.now() + '_' + name 44 | } 45 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "buffer-reuse-pool", 3 | "version": "1.0.0", 4 | "description": "An easy way to reuse buffers without much API ceremony", 5 | "main": "index.js", 6 | "dependencies": {}, 7 | "devDependencies": { 8 | "standard": "^11.0.0", 9 | "tape": "^4.9.0" 10 | }, 11 | "scripts": { 12 | "test": "standard && tape test.js" 13 | }, 14 | "repository": { 15 | "type": "git", 16 | "url": "https://github.com/mafintosh/buffer-reuse-pool.git" 17 | }, 18 | "author": "Mathias Buus (@mafintosh)", 19 | "license": "MIT", 20 | "bugs": { 21 | "url": "https://github.com/mafintosh/buffer-reuse-pool/issues" 22 | }, 23 | "homepage": "https://github.com/mafintosh/buffer-reuse-pool" 24 | } 25 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | const tape = require('tape') 2 | const reuse = require('./') 3 | 4 | tape('alloc pool and reuse', function (t) { 5 | const pool = reuse.pool(1024) 6 | const buf = pool.alloc() 7 | 8 | t.same(buf.length, 1024, 'length is pool length') 9 | t.ok(Buffer.isBuffer(buf), 'is buffer') 10 | 11 | const nextBuf = pool.alloc() 12 | t.ok(nextBuf !== buf, 'not same buffer') 13 | 14 | t.ok(reuse.free(buf), 'was freed') 15 | 16 | const nextNextBuf = pool.alloc() 17 | 18 | t.ok(nextNextBuf === buf, 'is same buffer') 19 | t.end() 20 | }) 21 | 22 | tape('slice and reuse', function (t) { 23 | const pool = reuse.pool(1024) 24 | const buf = pool.alloc() 25 | 26 | const slice = reuse.slice(buf, 10, 20) 27 | 28 | t.same(slice.length, 10, 'right length') 29 | for (var i = 0; i < 10; i++) slice[i] = i 30 | t.same(buf.slice(10, 20), slice, 'is a real slice') 31 | 32 | t.ok(reuse.free(slice), 'was freed') 33 | t.ok(buf === pool.alloc(), 'is same buffer') 34 | 35 | t.end() 36 | }) 37 | 38 | tape('free not reusable buffer', function (t) { 39 | const buf = Buffer.alloc(1024) 40 | 41 | t.ok(!reuse.free(buf), 'not freed') 42 | t.end() 43 | }) 44 | --------------------------------------------------------------------------------