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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@thi.ng/trie

v2.0.13

Published

Trie-based ES6-like Map data structures with prefix search/query support

Downloads

518

Readme

@thi.ng/trie

npm version npm downloads Mastodon Follow

[!NOTE] This is one of 214 standalone projects, maintained as part of the @thi.ng/umbrella monorepo and anti-framework.

🚀 Please help me to work full-time on these projects by sponsoring me on GitHub. Thank you! ❤️

About

Trie-based ES6-like Map data structures with prefix search/query support.

This package contains functionality which was previously part of and has been extracted from the @thi.ng/associative package.

TrieMap

Tries (also called Prefix maps) are useful data structures for search based use cases, auto-complete, text indexing etc. and provide partial key matching (prefixes), suffix iteration for a common prefix, longest matching prefix queries etc.

The implementations here too feature ES6 Map-like API, similar to other types in this package, with some further trie-specific additions.

import { defTrieMap } from "@thi.ng/trie";

// construct trie from given key-value pairs (optional)
const trie = defTrieMap([
  ["hey", "en"],
  ["hello", "en"],
  ["hallo", "de"],
  ["hallo", "de-at"],
  ["hola", "es"],
  ["hold", "en"],
  ["hej", "se"],
]);

// find longest known prefix given key
console.log(trie.knownPrefix("hole"));
// "hol"

// all known keys
console.log([...trie.keys()])
// [ "hold", "hola", "hallo", "hej", "hello", "hey" ]

// all keys starting with given prefix
console.log([...trie.keys("he")])
// [ "hej", "hello", "hey" ]

// suffixes of given key only
console.log([...trie.keys("he", false)])
// [ "j", "llo", "y" ]

// values of keys starting with prefix
console.log([...trie.values("hol")]);
// [ "en", "es" ]

MultiTrie

The MultiTrie is similar to TrieMap, but uses array-like keys and supports multiple values per key. Values are stored in sets whose implementation can be configured via ctor options (e.g. using custom ES6-like Sets with value-based equality semantics from the thi.ng/associative package).

import { defMultiTrie } from "@thi.ng/trie";
import { ArraySet } from "@thi.ng/associative";

// init w/ custom value set type (here purely for illustration)
const t = defMultiTrie<string, string>(null, { values: () => new ArraySet() });

t.add("to be or not to be".split(" "), 1);
t.add("to be or not to be".split(" "), 2);
t.add("to be and to live".split(" "), 3);

console.log(t.get("to be or not to be".split(" ")))
// Set(2) { 1, 2 }

console.log(t.knownPrefix(["to", "be", "not"]));
// [ "to", "be" ]

// suffixes for given prefix
console.log([...t.keys(["to", "be"], false)]);
// [["and", "to", "live"], ["or", "not", "to", "be"]]

Status

STABLE - used in production

Search or submit any issues for this package

Related packages

  • @thi.ng/associative - ES Map/Set-compatible implementations with customizable equality semantics & supporting operations

Installation

yarn add @thi.ng/trie

ESM import:

import * as trie from "@thi.ng/trie";

Browser ESM import:

<script type="module" src="https://esm.run/@thi.ng/trie"></script>

JSDelivr documentation

For Node.js REPL:

const trie = await import("@thi.ng/trie");

Package sizes (brotli'd, pre-treeshake): ESM: 1.14 KB

Dependencies

Note: @thi.ng/api is in most cases a type-only import (not used at runtime)

API

Generated API docs

Authors

If this project contributes to an academic publication, please cite it as:

@misc{thing-trie,
  title = "@thi.ng/trie",
  author = "Karsten Schmidt",
  note = "https://thi.ng/trie",
  year = 2020
}

License

© 2020 - 2026 Karsten Schmidt // Apache License 2.0