0.1.5 • Published 7 years ago

binary-ring-buffer v0.1.5

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

Binary Ring Buffer Build Status Coverage Status

Abstraction over JS ArrayBuffer. This provides an easy and space efficient way to write and read individual bits.

API

  • writeBits(value, numberOfBits) Writes a value into the buffer. Values are expected to be positive integers and less than 2^numberOfBits, no guarantees what will happen otherwise. Only accurate up to 53 bits at a time. Values are padded left with zeros, e.g. writeBits(3, 5) will write 00011.

  • readBits(numberOfBits) Read number of bits and return as a number. Exact up to 53 bits, accurate past it, but precision decaying as double floating point number does.

  • peakBits(numberOfBits) Read bits without advancing the read position.

  • clear Reset the buffer.

Example Write / Read individual bits

const BinaryRingBuffer = require('binary-ring-buffer');
let buf = new BinaryRingBuffer();
buf.writeBits(1, 1); // 1
buf.writeBits(0, 2); // 100
buf.writeBits(1, 1); // 1001
console.log(buf.readBits(4)); // '9'

Installation

npm i binary-ring-buffer

Benchmarks

You can run benchmarks yourself by running node benchmark.js

Testbuffernormal arrays
write bit2,861 ops/sec115,299 ops/sec
write and read bit1,653 ops/sec98,280 ops/sec
read and write while growing150 ops/sec314 ops/sec
write zeros799 ops/sec1,863 ops/sec

I/O for ring buffer is much slower than normal arrays in most cases. So wouldn't recommend it for high throughput tasks. Performance might be able to be improved by batching writes using normal arrays. The main advantage of the buffer is to use minimal memory.

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago