Skip to content

Instantly share code, notes, and snippets.

@fatso83
Created May 7, 2026 16:00
Show Gist options
  • Select an option

  • Save fatso83/d703c45d33e42ae9fd6ddf567d5ce855 to your computer and use it in GitHub Desktop.

Select an option

Save fatso83/d703c45d33e42ae9fd6ddf567d5ce855 to your computer and use it in GitHub Desktop.
JS compact ring buffer useful for logs
// 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;
}
};
};
@fatso83
Copy link
Copy Markdown
Author

fatso83 commented May 7, 2026

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment