Queue-Buffer
A flex buffer which behaves as a dynamic queue with a complete but limited Buffer API.
- Queue-Buffer extends Flex-Buffer.
- Tested on Node 0.8-0.12, latest iojs on Mac, Linux and Windows.
- Entities in the queue are kept in order and the only operations on the collection are the addition of entities to the rear terminal position, known as enqueue(write), and removal of entities from the front terminal position, known as dequeue(read).
Quick Start
npm install queue-buffer -S
then
QueueBuffer = ; buf = 10;buf;buf;buf;
API
QueueBuffer extends Flexbuffer, please see Flex-Buffer for more API info.
-
#### QueueBuffer.AUTO_RELEASE_THRESHOLD Auto release threshold(ms)
- type: { number }
-
#### constructor (arg, opts = {}) see FlexBuffer
-
param:
arg
{ number | Buffer | Array | string }The same arg as Buffer.
-
param:
opts
{ Object={} }options
-
option:
autoRelease
{ boolean }auto release useless space
-
option:
autoReleaseThreshold
{ number }auto release threshold(ms)
-
-
#### free () Release useless space which has been read.
-
#### move (size) Move current read offset.
-
param:
size
{ number }number of bytes to move, can be negative.
-
-
#### skip (size) Move current read offset forward.
-
param:
size
{ number }number of bytes to skip
-
-
#### rewind (size) Move current read offset backward.
-
param:
size
{ number }number of bytes to rewind
-
-
#### read (size) (alias: unshift, dequeue) Read bytes from the head of the buffer.
-
param:
size
{ number }number of bytes to read
-
return: { Buffer }
data
-
-
#### write (value, encoding = "utf8") (alias: push, enqueue) see FlexBuffer
-
param:
value
{ number | string | Array | Buffer }The value to write
-
param:
encoding
{ string="utf8" }string encoding
-
-
#### length length of the data
- type: { number }
All the native Buffer API is wrapped. However, read* methods can only read data from head, with no offset
argument.
Test
npm test
Benchmark
npm run benchmark
Environment: OS X 10.10.4, Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
io.js v2.4.0
-
Read
- Buffer x 721,399 ops/sec ±0.52% (93 runs sampled)
- FlexBuffer x 656,232 ops/sec ±0.87% (90 runs sampled)
-
wrapped native API
- Buffer x 17,708,521 ops/sec ±0.72% (93 runs sampled)
- FlexBuffer x 12,937,044 ops/sec ±0.70% (91 runs sampled)
io.js v3.0.0
see https://github.com/dracupid/flex-buffer#benchmark for more info
-
Read
- Buffer x 323,664 ops/sec ±0.68% (91 runs sampled)
- FlexBuffer x 320,600 ops/sec ±0.75% (93 runs sampled)
-
wrapped native API
- Buffer x 9,614,392 ops/sec ±0.70% (92 runs sampled)
- FlexBuffer x 7,115,334 ops/sec ±0.68% (93 runs sampled)
License
MIT@Jingchen Zhao