https://github.com/streamich/sonic-forest
High performance tree and sorted map implementations for JavaScript in TypeScript
https://github.com/streamich/sonic-forest
Last synced: 11 months ago
JSON representation
High performance tree and sorted map implementations for JavaScript in TypeScript
- Host: GitHub
- URL: https://github.com/streamich/sonic-forest
- Owner: streamich
- License: apache-2.0
- Created: 2024-04-20T10:42:05.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2025-08-10T14:14:27.000Z (11 months ago)
- Last Synced: 2025-08-10T16:22:12.281Z (11 months ago)
- Language: TypeScript
- Homepage: https://streamich.github.io/sonic-forest/
- Size: 2.17 MB
- Stars: 22
- Watchers: 1
- Forks: 2
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
README
# Sonic Forest
High performance (binary) tree and sorted map implementation for JavaScript in TypeScript.
## Features
- AVL tree implementation
- AVL sorted map implementation
- AVL sorted set implementation
- Red-black (RB) tree implementation
- Red-black (RB) tree sorted map implementation
- Left-leaning Red-black (LLRB) tree implementation
- Radix tree implementation (string keys)
- Binary radix tree implementation (Uint8Array keys)
- Splay tree implementation
- Various utility methods for binary trees
This package implements the fastest insertion into self-balancing binary tree out of any
NPM package. Both, AVL and Red-black tree insertion implementations of `sonic-forest` a faster
than inserts in [`js-sdsl`](https://www.npmjs.com/package/js-sdsl) implementation.
However, deletions from a binary tree are faster in `js-sdsl`. But, deletions in `sonic-forest`
delete exactly the node, which contains the key. Unlike, in `js-sdsl` and all other
binary tree libraries, where those libraries find the in-order-sucessor or -predecessor, which
is a leaf node, and delete that instead. As such, one can keep pointers to `sonic-forest` AVL
and Red-black tree nodes, and those pointers will stay valid even after deletions.
## Binary Radix Tree
The binary radix tree implementation supports `Uint8Array` keys, making it suitable for binary data like:
- Binary protocol routing
- File system paths as binary data
- Cryptographic hashes
- Network packet classification
- Any binary blob keys
### Key Features
- **Efficient slicing**: Uses `Slice` class to reference portions of `Uint8Array` without copying data
- **Prefix compression**: Automatically compresses common prefixes to save memory
- **Binary-safe**: Works with any byte sequence, including null bytes
- **Same API**: Provides similar interface to the string-based radix tree
### Usage Example
```typescript
import { BinaryRadixTree } from 'sonic-forest';
const tree = new BinaryRadixTree();
// Insert binary keys
tree.set(new Uint8Array([0x47, 0x45, 0x54, 0x20]), 'GET '); // "GET "
tree.set(new Uint8Array([0x50, 0x4F, 0x53, 0x54]), 'POST'); // "POST"
tree.set(new Uint8Array([0x50, 0x55, 0x54, 0x20]), 'PUT '); // "PUT "
// Retrieve values
console.log(tree.get(new Uint8Array([0x47, 0x45, 0x54, 0x20]))); // "GET "
// Delete keys
tree.delete(new Uint8Array([0x50, 0x4F, 0x53, 0x54])); // Remove POST
console.log(tree.size); // 2
```