Skip to content
This repository

JavaScript Circular Buffer

branch: master

Allow pop'd values to be gc'd

Changed .pop to remove the reference to the object. Also changed the
default value of .overflow to null. Changed how the default value of
rotateLeft/Right is detected.

The .pop method should remove the reference from the buffer, because
otherwise if it's a buffer of objects they'll never be garbage collected.
Since overflow is a function object, it makes sense that it starts out as
null. The methods rotateLeft and rotateRight should support receiving 0
(!cntr would evaluate to true and change it to 1 otherwise).
latest commit 0c8b979ceb
Matheus Valadares Matheus28 authored trevnorris committed
Octocat-spinner-32 speed Add speed tests November 23, 2012
Octocat-spinner-32 test Finished adding most tests November 30, 2012
Octocat-spinner-32 .gitignore begin adding info for testing April 11, 2012
Octocat-spinner-32 LICENSE Add license January 11, 2013
Octocat-spinner-32 README.md Made strong and unverified claims December 12, 2012
Octocat-spinner-32 cbuffer.js Allow pop'd values to be gc'd July 05, 2013
Octocat-spinner-32 package.json Now working on 0.1.5 November 26, 2012
README.md

JavaScript Circular Buffer Utility

The end goal of this project is to implement the entire JavaScript Array.prototype, and some additional utility methods, as a circular buffer.

Note: This is called a circular buffer because of what this library accomplishes, but is implemented as an Array. This may be confusing for Node users, which may want to use a true Buffer.

While the entire Array.prototype API is on the roadmap, it's not all quite here. Below is the currently implemented API.

Usage

It's simple. Just use it like you would use an Array.

new CBuffer(10);      // empty buffer with length of 10
new CBuffer(1,2,3,4); // buffer with length 4
CBuffer(5);           // For those who are really lazy, new is optional

Included are several non-standard niceties. Like if you want to catch when data is overwritten, just assign a function to the overflow variable and it will be called whenever a value is about to be overwritten and it will pass the value as the first argument:

var myBuff = CBuffer(4);
myBuff.overflow = function(data) {
    console.log(data);
};

myBuff.push(1,2,3,4); // nothing shows up yet
myBuff.push(5);       // log: 1

API

Mutator Methods

  • pop - Removes the last element from a circular buffer and returns that element.
  • push - Adds one or more elements to the end of a circular buffer and returns the new size.
  • reverse - Reverses the order of the elements of a circular buffer.
  • rotateLeft - Rotates all elements left 1, or n, times.
  • rotateRight - Rotates all elements right 1, or n, times.
  • shift - Removes the first element from a circular buffer and returns that element.
  • sort - Sorts the elements of a circular buffer.
  • unshift - Adds one or more elements to the front of a circular buffer and returns the new size.

Accessor Methods

  • indexOf - Returns the first (least) index of an element within the circular buffer equal to the specified value, or -1 if none is found.
  • lastIndexOf - Returns the last (greatest) index of an element within the circular buffer equal to the specified value, or -1 if none is found.

Iteration Methods

  • every - Returns true if every element in the circular buffer satisfies the provided testing function.
  • forEach - Calls a function for each element in the circular buffer.
  • some - Returns true if at least one element in the circular buffer satisfies the provided testing function.

Utility Methods

  • empty - Equivalent to setting Array.length = 0.
  • fill - Fill with passed argument. Also supports functions.
  • first - Returns first value in circular buffer.
  • last - Returns last value in circular buffer.
  • get - Get value at specific index.
  • set - Set value as specific index.
  • toArray - Return clean ordered array of buffer.
  • overflow - Set to function and will be called when data is about to be overwritten.
Something went wrong with that request. Please try again.