circular-buffer

Attempt at Circular Buffer - Javascript


Right! Here is my attempt at a circular buffer (For use in a graphing program, using the canvas element). Have yet to get round to testing it out.

Question is - Can anyone see any flaws in my logic? Or bottlenecks?

/**
*   A circular buffer class.
*   @To add value -> bufferObject.addValue(xValue, yValue);
*   @To get the First-in value use -> bufferObject.getValue(0);
*   @To get the Last-in value use -> bufferObject.getValue(bufferObject.length);
**/

var circularBuffer = function (bufferSize) {

    this.bufferSize = bufferSize;
    this.buffer = new Array(this.bufferSize); // After testing on jPerf -> 2 x 1D array seems fastest solution.

    this.end = 0;
    this.start = 0;

    // Adds values to array in circular.
    this.addValue = function(xValue, yValue) {
        this.buffer[this.end] = {x : xValue, y: yValue};
        if (this.end != this.bufferSize) this.end++;
        else this.end = 0;
        if(this.end == this.start) this.start ++;
    };

    // Returns a value from the buffer
    this.getValue = function(index) {

        var i = index+this.start;

        if(i >= this.bufferSize) i -= this.bufferSize; //Check here.

        return this.buffer[i]
    };

    // Returns the length of the buffer
    this.getLength = function() {
        if(this.start > this.end || this.start == this.bufferSize) {
            return this.xBuffer.length;
        } else {
            return this.end - this.start;
        }
    };

    // Returns true if the buffer has been initialized.
    this.isInitialized = function() {
        if(this.end != this.start) return true;
        else return false;
    };
}

Please feel free to reuse this code.

Updated twice (and tested!).


Solution

  • Update: Found another implementation Circular buffer in JavaScript

    Made class variables private, corrected old xBuffer reference. Will do more edits tonight.

    /**
    *   A circular buffer class.
    *   @To add value -> bufferObject.addValue(xValue, yValue);
    *   @To get the First-in value use -> bufferObject.getValue(0);
    *   @To get the Last-in value use -> bufferObject.getValue(bufferObject.length);
    **/
    
    var circularBuffer = function (buffer_size) {
    
        var bufferSize = buffer_size;
        var buffer = new Array(bufferSize); // After testing on jPerf -> 2 x 1D array seems fastest solution.
    
        var end = 0;
        var start = 0;
    
        // Adds values to array in circular.
        this.addValue = function(xValue, yValue) {
            buffer[end] = {x : xValue, y: yValue};
            if (end != bufferSize) end++;
            else end = 0;
            if(end == start) start++;
        };
    
        // Returns a value from the buffer
        this.getValue = function(index) {
    
            var i = index+start;
    
            if(i >= bufferSize) i -= bufferSize; //Check here.
    
            return buffer[i];
        };
    
        // Returns the length of the buffer
        this.getLength = function() {
            if(start > end || start == bufferSize) {
                return buffer.length;
            } else {
                return end - start;
            }
        };
    
        // Returns true if the buffer has been initialized.
        this.isInitialized = function() {
            return (end != start) ? true : false;
        };
    }