Created
May 7, 2026 16:00
-
-
Save fatso83/d703c45d33e42ae9fd6ddf567d5ce855 to your computer and use it in GitHub Desktop.
JS compact ring buffer useful for logs
This file contains hidden or 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
| // basically https://stackoverflow.com/a/4774081/32164693 | |
| function createRingBuffer(length){ | |
| let pointer = 0; | |
| const buffer = []; | |
| return { | |
| get(key){return buffer[key];}, | |
| push(item){ | |
| buffer[pointer] = item; | |
| pointer = (length + pointer +1) % length; | |
| } | |
| }; | |
| }; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if you need to support concurrent SPSC: ringbuf.js, but where both operations are done in the same thread (as is the case when using a web worker to process logs), the above is sufficient.