npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

prefix-tries

v1.1.1

Published

A TypeScript library offering efficient standard (prefix) and compressed (radix) tries for optimized string storage and operations, featuring trie-map variants for key-value storage.

Readme

Prefix Tries

NPM Version Coverage Status

A high-performance TypeScript library for efficient string manipulation and storage using tree-based data structures. This library provides:

Core Data Structures

  • Standard Trie (also known as "prefix trie" or "prefix tree")

    • Classic implementation optimized for straightforward usage
    • Ideal for most common string storage and retrieval needs
    • Perfect for smaller to medium-sized datasets
  • Compressed Trie (also known as "radix trie" or "radix tree")

    • Memory-optimized implementation that merges single-child nodes
    • Significantly reduces memory footprint for large datasets
    • Maintains fast lookup performance while using less space

Extended Functionality

Both implementations are available as pure tries for string storage and trie-maps for key-value associations, making this library suitable for a wide range of applications from autocomplete systems to routing tables.

Key Features

Data Structure Options

  • Standard Tries: Traditional prefix tree implementation for straightforward string operations
  • Compressed Tries: Space-efficient radix tree variant for optimized memory usage
  • Trie Maps: Associate values with strings while maintaining prefix-based capabilities
  • Compressed Trie Maps: Combine key-value storage with memory optimization

Core Capabilities

  • Efficient Prefix Operations: Lightning-fast prefix-based searches and pattern matching
  • Order Preservation: Maintains insertion order for predictable iteration
  • Flexible Search: Find all strings sharing common prefixes instantly
  • Memory Efficiency: Optimized data structures for reduced memory consumption

Developer Experience

  • Type Safety: Full TypeScript support with comprehensive type definitions
  • Intuitive API: Consistent interface across all implementations
  • Rich Documentation: Detailed API docs and usage examples

Table of Contents

System Requirements

| Package | Version | | ----------- | ---------- | | Node.js | ≥ 18.0.0 | | npm | ≥ 8.0.0 |

Installation

Install via npm

npm install prefix-tries

Install via yarn

yarn add prefix-tries

Install via pnpm

pnpm install prefix-tries

Importing Modules

The library provides both standard and memory-optimized implementations of tries and trie-maps. You can import the specific classes you need:

import {
  // Concrete Classes
  Trie, // Standard trie implementation.
  TrieMap, // Standard trie-map implementation.
  CompressedTrie, // Memory-optimized trie implementation.
  CompressedTrieMap, // Memory-optimized trie-map implementation.

  // Abstract Base Classes
  AbstractTrie, // Base class for trie implementations.
  AbstractTrieMap, // Base class for trie-map implementations.
} from 'prefix-tries';

Getting Started

Here's a quick guide to help you get started with the library.

Using Tries

A basic example using the memory-optimized CompressedTrie:

import { CompressedTrie } from 'prefix-tries';

// Initialize a trie with some words.
const words = ['apple', 'orange'];
const trie = new CompressedTrie(words);

// Add a new word.
trie.add('apricot');

// Count total words.
console.log(trie.size); // 3

// Check if word exists.
console.log(trie.has('orange')); // true

// Find words with prefix 'ap'.
console.log(trie.find('ap')); // ['apple', 'apricot']

// Iterate through all words (in insertion order).
for (let word of trie) {
  console.log(word);
  /*
    Output:
    apple
    orange
    apricot
  */
}

// Successful word removal.
console.log(trie.delete('apple')); // true

// Check the updated total word count.
console.log(trie.size); // 2

// Check for word that no longer exists.
console.log(trie.has('apple')); // false

// Clear all words.
trie.clear();
console.log(trie.size); // 0

Using Trie Maps

A basic example using the memory-optimized CompressedTrieMap:

import { CompressedTrieMap } from 'prefix-tries';

// Initialize a trie-map with key-value pairs.
const wordValuePairs = [
  ['apple', 'value1'],
  ['orange', 'value2'],
];
const trieMap = new CompressedTrieMap(wordValuePairs);

// Add a new key-value pair.
trieMap.set('apricot', 'value3');

// Count total entries.
console.log(trieMap.size); // 3

// Check if key exists.
console.log(trieMap.has('orange')); // true

// Retrieve value by key.
console.log(trieMap.get('apricot')); // 'value3'

// Find entries with prefix 'ap'.
console.log(trieMap.find('ap')); // [['apple', 'value1'], ['apricot', 'value3']]

// Iterate through all entries (in insertion order).
for (let entry of trieMap) {
  console.log(entry);
  /*
    Output:
    ['apple', 'value1']
    ['orange', 'value2']
    ['apricot', 'value3']
  */
}

// Iterate through all keys.
for (let word of trieMap.keys()) {
  console.log(word);
  /*
    Output:
    apple
    orange
    apricot
  */
}

// Iterate through all values.
for (let value of trieMap.values()) {
  console.log(value);
  /*
    Output:
    value1
    value2
    value3
  */
}

// Successful entry removal.
console.log(trieMap.delete('apple')); // true

// Check the updated total word count.
console.log(trieMap.size); // 2

// Check for word that no longer exists.
console.log(trieMap.has('apple')); // false
console.log(trieMap.get('apple')); // undefined

// Clear all entries.
trieMap.clear();
console.log(trieMap.size); // 0

Code documentation

The complete API reference of the library with detailed examples is available at the code documentation site.

Issues and Support

If you encounter any issues or have questions, please open an issue.

License

This project is licensed under the MIT License.