Registry / storage / circular_buffer_js

circular_buffer_js

JSON →
library1.10.0jsnpmunverified

A TypeScript/JavaScript implementation of a circular buffer (ring buffer, cyclic queue) with 100% code and property coverage. Current stable version is 1.10.0. Released regularly with no notable release cadence. Key differentiators: zero runtime dependencies, multiple build outputs (ESM module, CommonJS, IIFE), TypeScript types included, and extensive testing. Compared to alternatives like 'buffer' or simple arrays, it provides a fixed-size FIFO data structure with efficient push/pop and additional utility methods like shove, resize, and reverse.

npm install circular_buffer_js
INSTALL
IMPORT
SIG · CIRCULAR_BUFFER_JS
C
circular_buffer_js
storagejavascriptv1.10.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

circular_buffer
import { circular_buffer } from 'circular_buffer_js'
import circular_buffer from 'circular_buffer_js'
The package exports a named class, not a default export. Both ESM and CJS modules support named import.
circular_buffer
const { circular_buffer } = require('circular_buffer_js')
const circular_buffer = require('circular_buffer_js')
In CommonJS, destructure the require to get the named export.
circular_buffer
const cb = require('circular_buffer_js').circular_buffer
const cb = require('circular_buffer_js')
Alternatively, access the property directly from the module object.

Demonstrates creating a circular buffer, pushing/popping elements, checking state, shoving with overflow, toArray, resize, and clear.

import { circular_buffer } from 'circular_buffer_js'; // Create a buffer of size 5 const buf = new circular_buffer(5); // Add elements buf.push('a'); buf.push('b'); buf.push('c'); // Access elements console.log(buf.first); // 'a' console.log(buf.last); // 'c' console.log(buf.at(1)); // 'b' // Check state console.log(buf.length); // 3 console.log(buf.isFull); // false console.log(buf.isEmpty); // false console.log(buf.available);// 2 // Remove element const removed = buf.pop(); // 'a' console.log(buf.length); // 2 // Fill buffer to capacity buf.push('d'); buf.push('e'); buf.push('f'); // buffer is now full // Shove adds element, removing oldest if full const evicted = buf.shove('g'); // 'b' is evicted console.log(evicted); // 'b' // Convert to array console.log(buf.toArray()); // ['c', 'd', 'e', 'f', 'g'] // Resize buffer buf.resize(3); // keeps first 3: ['c', 'd', 'e'] // Clear buffer buf.clear(); console.log(buf.isEmpty); // true
Debug
Known issues
deprecatedThe resize method second argument pE is optional; default behavior truncates from the end. In version 1.x, if pE is true, truncates from the front.
fix
Specify pE explicitly if you want to control truncation direction.
affects: >=1.0.0
gotchaUsing 'new circular_buffer(0)' will create a buffer with capacity 0, which is valid but can lead to unexpected behavior (push will always throw or shove will evict immediately).
fix
Ensure capacity is greater than 0 for normal use.
affects: >=1.0.0
gotchaThe capacity is fixed at construction time; resize must be used to change it. Attempting to push beyond capacity without using shove will throw an error.
fix
Use 'shove' to automatically evict oldest element when full, or call 'resize' to increase capacity.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: circular_buffer is not a constructor
Incorrect import - got the default export instead of named export.
fix
Use 'import { circular_buffer } from 'circular_buffer_js'' (with curly braces).
Error: Buffer is full
Attempted to push to a full buffer without using shove.
fix
Either check 'isFull' before pushing, or use 'shove' method instead of 'push'.
TypeError: cb.push is not a function
Imported the wrong module or variable is undefined.
fix
Ensure you instantiate a circular_buffer instance: 'const cb = new circular_buffer(size)'.
Upgrade
Version history
1.10.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
26 hits · last 30 days
node
24
Resources
circular_buffer_js — npm install circular_buffer_js · libregistry