backed by one node per distinct segment
prefix, each holding its children in a Map keyed by the next segment.
Shared key prefixes are stored once and looked up once, which is what turns
longest-prefix resolution into a single top-down walk. Prefer
compressedSegmentedTrie
when keys are deep or mostly non-branching,
which is when packing segment runs into edge labels pays for its split
bookkeeping; prefer a plain Map<string, V> keyed on the joined string
when only exact matches are needed.
@param ― entries - Key-value pairs to store, in iteration order. A repeated
key overwrites the earlier value, mirroring Map, and entries before an
invalid one stay stored.
@throws ― {TrieInvalidInputError} When an entry key is not an array
of strings.
backed by one node per distinct segment
prefix, each holding its children in a Map keyed by the next segment.
Shared key prefixes are stored once and looked up once, which is what turns
longest-prefix resolution into a single top-down walk. Prefer
compressedSegmentedTrie
when keys are deep or mostly non-branching,
which is when packing segment runs into edge labels pays for its split
bookkeeping; prefer a plain Map<string, V> keyed on the joined string
when only exact matches are needed.
@param ― entries - Key-value pairs to store, in iteration order. A repeated
key overwrites the earlier value, mirroring Map, and entries before an
invalid one stay stored.
@throws ― {TrieInvalidInputError} When an entry key is not an array
of strings.
Calls a defined callback function on each element of an array, and returns an array that contains the results.
@param ― callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
Split a string into substrings using the specified separator and return them as an array.
@param ― separator A string that identifies character or characters to use in separating the string. If omitted, a single-element array containing the entire string is returned.
@param ― limit A value used to limit the number of elements returned in the array.
Resolve the longest stored key that is a segment-wise prefix of the query,
including the query itself and the empty key. O(m) expected.
The walk stops at the first missing segment, because no deeper key can
prefix the query. Returns the matched
key as a fresh prefix of the query array together with its value, or
undefined when no stored key prefixes the query.
longestPrefix('GOOGLE_ANALYTICS_RUN_REPORT'.
String.toLowerCase(): string
Converts all the alphabetic characters in a string to lowercase.
Split a string into substrings using the specified separator and return them as an array.
@param ― separator A string that identifies character or characters to use in separating the string. If omitted, a single-element array containing the entire string is returned.
@param ― limit A value used to limit the number of elements returned in the array.
Resolve the longest stored key that is a segment-wise prefix of the query,
including the query itself and the empty key. O(m) expected.
The walk stops at the first missing segment, because no deeper key can
prefix the query. Returns the matched
key as a fresh prefix of the query array together with its value, or
undefined when no stored key prefixes the query.
Resolve the longest stored key that is a segment-wise prefix of the query,
including the query itself and the empty key. O(m) expected.
The walk stops at the first missing segment, because no deeper key can
prefix the query. Returns the matched
key as a fresh prefix of the query array together with its value, or
undefined when no stored key prefixes the query.
keys average four segments or fewer and most segments are keys themselves, so compression removes under 12% of nodes and only adds edge splits
one Map lookup per segment; 126,343 nodes against 111,987 for the compressed layout on 100,000 underscore slugs of 1-4 segments
compressedSegmentedTrie
kind compressed
keys average more than four segments and most of them do not branch, where node count falls around 7x and deep misses resolve in 9.78-13.19 ms against 13.19-16.71 ms uncompressed at 100,000 slash paths
one Map lookup per edge plus a segment-by-segment comparison of the skipped label, and an edge split on any insert that lands inside an edge
Every error this module throws is TrieInvalidInputError or TrieParseError, with a code from a closed, exported list. Branch on the code; the message is for people.
Use knotkit/trie for this task. If Knotkit is missing, follow https://knotkit.jkomyno.dev/getting-started.md#install-from-source. Import from 'knotkit/trie'.
Exports:
- `uncompressedSegmentedTrie` → `SegmentedTrie<V>`: Segment trie with one node per segment, each node holding a `Map` of children in insertion order.
- `compressedSegmentedTrie` → `SegmentedTrie<V>`: Radix-compressed segment trie: one node per branch point or key end, with the skipped segments carried as an edge label.
Pick the layout by these rules:
- `uncompressedSegmentedTrie` (kind 'uncompressed') when keys average four segments or fewer and most segments are keys themselves, so compression removes under 12% of nodes and only adds edge splits
- `compressedSegmentedTrie` (kind 'compressed') when keys average more than four segments and most of them do not branch, where node count falls around 7x and deep misses resolve in 9.78-13.19 ms against 13.19-16.71 ms uncompressed at 100,000 slash paths
Inputs and their sources:
- value encoding for a binary artifact, which no artifact carries back: `ElementCodec<V>` from `knotkit/wire`
- value validation for a JSON artifact, whose projection carries no value types: `(value: unknown) => value is V` from `the caller`
- key segmentation, including the delimiter and any case or Unicode normalization, which the trie never performs: `(query: string) => readonly string[]` from `the caller`
Use something else when:
- look up a value by an exact key only, never by a prefix → Map
- decide the delimiter, or normalize case and Unicode before matching → String.prototype.split and String.prototype.toLowerCase before building or querying keys
- match a string against wildcards, character classes, or a range → RegExp
- iterate stored keys in sorted order → Array.prototype.sort over Map.keys()
- enumerate stored string keys that begin with a query prefix → knotkit/prefix-index
- find keys sharing a suffix, or containing a substring → a scan over Map.keys() with String.prototype.includes, or a suffix automaton
Errors are TrieInvalidInputError or TrieParseError with a `code` property. Branch on `error.code`, never on the message. Save and load through `knotkit/trie/wire` (serializeSegmentedTrie, deserializeSegmentedTrie, serializeSegmentedTrieJson, deserializeSegmentedTrieJson).
Full page: https://knotkit.jkomyno.dev/modules/trie.md
Types: node_modules/knotkit/trie/ and https://knotkit.jkomyno.dev/reference/knotkit/trie/