JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 88779
  • Score
    100M100P100Q164542F
  • License Apache-2.0

High-performance (binary) tree and sorted map implementation (AVL, Splay, Radix, Red-Black)

Package Exports

  • sonic-forest/lib/avl/AvlMap
  • sonic-forest/lib/avl/AvlMap.js
  • sonic-forest/lib/print/printBinary
  • sonic-forest/lib/print/printBinary.js
  • sonic-forest/lib/print/printTree
  • sonic-forest/lib/print/printTree.js
  • sonic-forest/lib/print/types
  • sonic-forest/lib/print/types.js
  • sonic-forest/lib/radix/RadixTree
  • sonic-forest/lib/radix/RadixTree.js
  • sonic-forest/lib/splay/util
  • sonic-forest/lib/splay/util.js
  • sonic-forest/lib/splay/util2
  • sonic-forest/lib/splay/util2.js
  • sonic-forest/lib/util
  • sonic-forest/lib/util/index.js
  • sonic-forest/lib/util2
  • sonic-forest/lib/util2.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (sonic-forest) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

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 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

import { BinaryRadixTree } from 'sonic-forest';

const tree = new BinaryRadixTree<string>();

// 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