Snapshot an iterable of strings as sorted, distinct keys. Keys compare by UTF-16 code units.
Build takes expected O(n) set operations for n input keys and O(u log(u + 1)) comparisons for u distinct keys.
Retained space is O(u), excluding the strings themselves. Errors from the input iterable propagate.
Snapshot an iterable of strings as sorted, distinct keys. Keys compare by UTF-16 code units.
Build takes expected O(n) set operations for n input keys and O(u log(u + 1)) comparisons for u distinct keys.
Retained space is O(u), excluding the strings themselves. Errors from the input iterable propagate.
Lazily yield up to limit distinct keys beginning with prefix, in ascending order.
The empty prefix matches every key. Zero yields no keys without searching.
For n distinct keys and k results, iteration takes O(log(n + 1) + k) string comparisons and
O(1) extra space. The cost of comparing strings depends on their lengths.
@throws ― {PrefixIndexInvalidInputError} With INVALID_LIMIT if the limit is outside the nonnegative safe integers.
Lazily yield up to limit distinct keys beginning with prefix, in ascending order.
The empty prefix matches every key. Zero yields no keys without searching.
For n distinct keys and k results, iteration takes O(log(n + 1) + k) string comparisons and
O(1) extra space. The cost of comparing strings depends on their lengths.
@throws ― {PrefixIndexInvalidInputError} With INVALID_LIMIT if the limit is outside the nonnegative safe integers.
Snapshot an iterable of strings as sorted, distinct keys. Keys compare by UTF-16 code units.
Build takes expected O(n) set operations for n input keys and O(u log(u + 1)) comparisons for u distinct keys.
Retained space is O(u), excluding the strings themselves. Errors from the input iterable propagate.
Build a read-only counted index from caller-tokenized documents. The selector runs once per input record.
The index retains terms, counts, and document positions. It can discard the source records after construction.
Expected O(n + t) build time for n documents and t token occurrences, plus the selector's work.
Build workspace and retained storage are O(n + v + p) for v vocabulary terms and p distinct postings.
@throws ― {TermIndexInvalidInputError} With INVALID_TERMS for a selector result other than an array of strings
or MAX_CAPACITY_EXCEEDED when corpus counts or packed storage exceed their bounds.
Errors thrown by the input iterable and selector propagate.
Build a read-only counted index from caller-tokenized documents. The selector runs once per input record.
The index retains terms, counts, and document positions. It can discard the source records after construction.
Expected O(n + t) build time for n documents and t token occurrences, plus the selector's work.
Build workspace and retained storage are O(n + v + p) for v vocabulary terms and p distinct postings.
@throws ― {TermIndexInvalidInputError} With INVALID_TERMS for a selector result other than an array of strings
or MAX_CAPACITY_EXCEEDED when corpus counts or packed storage exceed their bounds.
Errors thrown by the input iterable and selector propagate.
Snapshot an iterable of strings as sorted, distinct keys. Keys compare by UTF-16 code units.
Build takes expected O(n) set operations for n input keys and O(u log(u + 1)) comparisons for u distinct keys.
Retained space is O(u), excluding the strings themselves. Errors from the input iterable propagate.
Lazily yield up to limit distinct keys beginning with prefix, in ascending order.
The empty prefix matches every key. Zero yields no keys without searching.
For n distinct keys and k results, iteration takes O(log(n + 1) + k) string comparisons and
O(1) extra space. The cost of comparing strings depends on their lengths.
@throws ― {PrefixIndexInvalidInputError} With INVALID_LIMIT if the limit is outside the nonnegative safe integers.
Rank documents matching any supplied exact term. Ties in score favor lower IDs.
Repeated query terms contribute once. The returned array and hits are independent of the index.
@throws ― {TermIndexInvalidInputError} With INVALID_LIMIT for a limit outside the nonnegative safe integers
or INVALID_TERMS for an argument other than an array of strings. Eligibility accessor errors propagate.
search(
constterms:string[]
terms, {
TermSearchOptions.limit?: number |undefined
Maximum number of hits. Defaults to 10. Zero yields no hits.
Every error this module throws is PrefixIndexInvalidInputError, with a code from a closed, exported list. Branch on the code; the message is for people.
Measured by the suite in packages/prefix-index/__tests__/bench. The numbers come from one machine, so run the suite on yours before deciding on speed alone.
Use knotkit/prefix-index for this task. If Knotkit is missing, follow https://knotkit.jkomyno.dev/getting-started.md#install-from-source. Import from 'knotkit/prefix-index'.
Exports:
- `prefixIndex` → `PrefixIndex`: Snapshot distinct strings and enumerate at most the requested number of keys beginning with a prefix.
Inputs and their sources:
- string keys to index: `Iterable<string>` from `the caller`
Use something else when:
- find a stored delimiter-separated key that prefixes the query → knotkit/trie
- update keys between ordered range queries → knotkit/ordered-map
- rank documents after expanding the query → knotkit/term-index
Errors are PrefixIndexInvalidInputError with a `code` property. Branch on `error.code`, never on the message.
Full page: https://knotkit.jkomyno.dev/modules/prefix-index.md
Types: node_modules/knotkit/prefix-index/ and https://knotkit.jkomyno.dev/reference/knotkit/prefix-index/