Created
August 20, 2017 19:58
-
-
Save ChrisCates/a4ae88ebac8eb93507a7f735207c1c08 to your computer and use it in GitHub Desktop.
Ring Buffer in Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RingBuffer { | |
constructor(size) { | |
this.size = size; | |
this.pointer = 0; | |
this.buffer = []; | |
} | |
get(key) { | |
return this.buffer[key]; | |
} | |
push(item) { | |
this.buffer[this.pointer] = item; | |
this.pointer = (this.pointer + 1) % this.size; | |
return item; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment