https://github.com/oliviacarlisle/dslib-core
A robust and efficient data structures library.
https://github.com/oliviacarlisle/dslib-core
data-structures dequeue enqueue fifo javascript queue typescript
Last synced: 5 months ago
JSON representation
A robust and efficient data structures library.
- Host: GitHub
- URL: https://github.com/oliviacarlisle/dslib-core
- Owner: oliviacarlisle
- License: mit
- Created: 2024-10-04T22:27:05.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-07T19:19:14.000Z (9 months ago)
- Last Synced: 2025-05-10T10:46:06.197Z (5 months ago)
- Topics: data-structures, dequeue, enqueue, fifo, javascript, queue, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/dslib-core
- Size: 195 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# dslib-core
[](https://www.npmjs.com/package/dslib-core)
[](https://www.npmjs.com/package/dslib-core)
[](https://github.com/oliviacarlisle/dslib-core/actions/)
[](https://codecov.io/gh/oliviacarlisle/dslib-core)
[](https://opensource.org/licenses/MIT)
[](https://packagephobia.com/result?p=dslib-core)
[](https://bundlejs.com/?q=dslib-core)
[](https://bundlejs.com/?q=dslib-core)A robust and efficient data structures library.
## âĻ Features
- ð§° **Foundational**: Growing collection of essential data structures
- ðĄïļ **Reliable**: Robust stability with 100% test coverage
- ⥠**High-Performance**: Optimized for efficiency in demanding applications
- â°ïļ **Scalable**: Adaptable for projects of any size
- ðŠķ **Lightweight**: Zero dependencies, keeping your project lean
- ð· **TypeScript Native**: Full type safety and intelligent code completion## Table of Contents
- [ðĄ Why dslib-core?](#-why-dslib-core)
- [ðĶ Installation](#-installation)
- [ð Getting Started](#-getting-started)
- [ðïļ Data Structures](#%EF%B8%8F-data-structures)
- [ð Queue](#-queue)
- [ð Contributing](#-contributing)
- [âïļ License](#%EF%B8%8F-license)## ðĄ Why dslib-core?
`dslib-core` is your go-to toolkit for advanced data structures in TypeScript and JavaScript. It bridges the gap left by standard libraries, empowering developers with the tools they need for efficient data management and manipulation.
Whether you're building a complex algorithm or optimizing application performance, `dslib-core` provides the building blocks you need to succeed.
> **Note:** Currently only compatible with ESM projects.
## ðĶ Installation
Install the package via npm:
```bash
npm install dslib-core
```## ð Getting Started
Import using ESM syntax:
```typescript
import { Queue } from 'dslib-core';
```Quick start example:
```typescript
const queue = new Queue();
queue.enqueue(3);
console.log(queue.dequeue()); // Output: 3
```### ð§ Using `dslib-core` in CommonJS Projects
`dslib-core` is primarily designed for ESM (ECMAScript Module) environments. However, you can still use it in CommonJS projects with a few adjustments.
To use `dslib-core` in a CommonJS project, we suggest using a dynamic `import()` within an async IIFE (Immediately Invoked Function Expression). Here's an example:
```typescript
// CommonJS project example
(async function () {
const { Queue } = await import('dslib-core');// Your code here
const queue = new Queue();
queue.enqueue(1);
console.log(queue.dequeue()); // Output: 1
})();
```This approach allows you to use the ESM-native `dslib-core` package within your CommonJS environment while maintaining asynchronous module loading.
## ðïļ Data Structures
### ð Queue
Implemented using a circular buffer to ensure efficient enqueue and dequeue operations.
#### Features
- **Performance:** Enqueue and dequeue operations with amortized O(1) complexity.
- **Dynamic Resizing:** Automatically resizes to manage memory usage efficiently.
- **Iterable**: Implements the `Iterable` interface, allowing you to use the queue with `for...of` loops, spread (`...`) syntax and other iterable contexts.
- **Enhanced Runtime Privacy**: Leverages ES2022 private class fields (`#`) for robust encapsulation, ensuring data privacy during execution.
- **Type Safety:** Fully typed for TypeScript, ensuring type safety and IntelliSense support.
- **Versatility:** Suitable for handling numbers, strings, objects, and more.#### Methods
- `enqueue(item: T): number`
- Adds an item to the back of the queue.
- **Time complexity:** amortized O(1).
- **Parameters:**
- `item: T` - The item to add to the queue.
- **Returns**: The new size of the queue.
- `dequeue(): T | undefined`
- Removes and returns the item at the front of the queue.
- **Time complexity:** amortized O(1).
- **Returns**: The item at the front of the queue, or `undefined` if the queue is empty.
- `peek(): T | undefined`
- Returns the item at the front without removing it.
- **Time complexity:** guaranteed O(1).
- **Returns:** The item at the front of the queue, or `undefined` if the queue is empty.
- `get(index: number): T | undefined`
- Retrieves the element at the specified index without removing it.
- **Time complexity:** guaranteed O(1) for any index.
- **Parameters:**
- `index: number` - The zero-based index of the element to retrieve.
- **Returns:** The item at the front of the queue, or `undefined` if the index is out of bounds.
- `size: number` _(Getter)_
- Returns the number of items in the queue.
- `internalSize: number` _(Getter)_
- Returns the internal capacity of the queue's buffer (useful for debugging or analysis).
- `clear(): void`
- Removes all items from the queue.
- `[Symbol.iterator](): Iterator`
- Returns an iterator over the elements in the queue (allows for use with `for...of` loops and spread (`...`) syntax).
- `entries(): Iterator<[T, number]>`
- Returns an iterator over the elements in the queue along with their indices.
- **Yields:** Tuples of `[element, index]`.
- `toString(): string`
- Returns a string representation of the queue.
- **Format:** `Queue(size) { item1, item2, ... }`
- `forEach(callbackFn: (value: T, index: number, queue: Queue) => void): void`
- Executes a provided function once for each element in the queue.
- **Parameters:**
- `callbackFn` - Function to execute for each element, taking three arguments:
- `value: T` - The current element being processed.
- `index: number` - The index of the current element.
- `queue: Queue` - The queue object being traversed.#### Example Usage
```typescript
import { Queue } from 'dslib-core';const queue = new Queue();
queue.enqueue('first');
queue.enqueue('second');
queue.enqueue('third');console.log(queue.peek()); // Output: 'first'
console.log(queue.get(1)); // Output: 'second'
console.log(queue.size); // Output: 3queue.forEach((value, index) => {
console.log(`Index ${index}: ${value}`);
});
// Output:
// Index 0: first
// Index 1: second
// Index 2: thirdfor (const item of queue) {
console.log(item);
}
// Output:
// first
// second
// thirdfor (const [item, i] of queue.entries()) {
console.log(`Index ${i}: ${item}`);
}
// Output:
// Index 0: first
// Index 1: second
// Index 2: thirdconst arr = [...queue];
console.log(arr.length); // Output: 3console.log(queue.dequeue()); // Output: 'first'
console.log(queue.size); // Output: 2console.log(queue.toString()); // Output: 'Queue(2) { 'second', 'third' }'
queue.clear();
console.log(queue.size); // Output: 0
console.log(queue.dequeue()); // Output: undefined
```#### Performance
**Performance:** `enqueue` and `dequeue` methods offer performance comparable to the built-in array `push` and `pop` methods.
**Benchmark Results:**
```bash
âââââââââââŽââââââââââââŽâââââââââââââââŽâââââââââââââââââââââŽââââââââââââŽâââââââââââ
â (index) â Task Name â ops/sec â Average Time (ns) â Margin â Samples â
âââââââââââžââââââââââââžâââââââââââââââžâââââââââââââââââââââžââââââââââââžâââââââââââĪ
â 0 â 'enqueue' â '27,675,706' â 36.132772689503845 â 'Âą14.28%' â 13838019 â
â 1 â 'push' â '26,611,405' â 37.5778722674608 â 'Âą9.86%' â 13665449 â
âââââââââââīââââââââââââīâââââââââââââââīâââââââââââââââââââââīââââââââââââīâââââââââââ
âââââââââââŽââââââââââââŽâââââââââââââââŽâââââââââââââââââââââŽâââââââââââŽââââââââââ
â (index) â Task Name â ops/sec â Average Time (ns) â Margin â Samples â
âââââââââââžââââââââââââžâââââââââââââââžâââââââââââââââââââââžâââââââââââžââââââââââĪ
â 0 â 'pop' â '36,840,562' â 27.143994127022697 â 'Âą0.93%' â 7368113 â
â 1 â 'shift' â '797' â 1253180.9375000647 â 'Âą1.20%' â 160 â
â 2 â 'dequeue' â '33,964,152' â 29.442807571542765 â 'Âą8.38%' â 6792831 â
âââââââââââīââââââââââââīâââââââââââââââīâââââââââââââââââââââīâââââââââââīââââââââââ
```- The first table compares the performance of the built-in `push` method (on an empty array) and the `enqueue` method (on an empty `Queue`). Benchmarks tasks run sequentially for 500ms each.
- The second table compares the performance of the built-in `pop` and `shift` methods, and the `dequeue` method. Each benchmark task starts with an array/queue (as applicable) of size `2^24` (approx. 16 million). Benchmarks tasks run sequentially for 200ms each.
- Demonstrates similar efficiency to native array methods under high load, with a massive improvement over the buit-in `shift` method.> **_Note:_** The above benchmarks are run using items of type `object` in order to better simulate real-world scenarios. Using primitive data types like `string` or `number` would generally result in faster performance.
_Benchmarks conducted on 10/21/2024 using `tinybench` on a MacBook Pro (M3 Pro) using the following versions:_
```bash
> tsx --versiontsx v4.19.1
node v20.17.0
```To benchmark with your setup, clone this repo and run:
```bash
npm install
npm run bench
```## ð Contributing
Contributions are welcome! If you have ideas, suggestions, or find any issues, please open an [issue](https://github.com/oliviacarlisle/dslib-core/issues) or submit a [pull request](https://github.com/oliviacarlisle/dslib-core/pulls).
### Development Setup
Clone the repository:
```bash
git clone https://github.com/oliviacarlisle/dslib-core.git
```Install dependencies:
```bash
cd dslib-core
npm install
```Run tests:
```bash
npm test
```## âïļ License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
---
_Feel free to reach out if you have any questions or need assistance!_