Skip to content

knotkit/prefix-index

Enumerates a bounded number of distinct strings that start with a prefix in UTF-16 order

import {
function prefixIndex(input: Iterable<string>): PrefixIndex

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.

prefixIndex
} from 'knotkit/prefix-index'
const
const commands: PrefixIndex
commands
=
function prefixIndex(input: Iterable<string>): PrefixIndex

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.

prefixIndex
(['cache', 'compile', 'cat', 'cache', 'copy'])
var Array: ArrayConstructor
Array
.
ArrayConstructor.from<string>(iterable: Iterable<string> | ArrayLike<string>): string[] (+3 overloads)

Creates an array from an iterable object.

@param ― iterable An iterable object to convert to an array.

from
(
const commands: PrefixIndex
commands
.
PrefixIndex.keysWithPrefix: (prefix: string, limit: number) => IterableIterator<string>

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.

keysWithPrefix
('ca', 2)) // ['cache', 'cat']
var Array: ArrayConstructor
Array
.
ArrayConstructor.from<string>(iterable: Iterable<string> | ArrayLike<string>): string[] (+3 overloads)

Creates an array from an iterable object.

@param ― iterable An iterable object to convert to an array.

from
(
const commands: PrefixIndex
commands
.
PrefixIndex.keysWithPrefix: (prefix: string, limit: number) => IterableIterator<string>

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.

keysWithPrefix
('co', 1)) // ['compile']
prefix-index.ts
import {
function prefixIndex(input: Iterable<string>): PrefixIndex

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.

prefixIndex
} from 'knotkit/prefix-index'
import {
function termIndex<R>(records: Iterable<R>, termsOf: (record: R, id: number) => readonly string[]): TermIndex

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.

termIndex
} from 'knotkit/term-index'
const
const docs: string[][]
docs
= [
['cache', 'setup'],
['caching', 'rules'],
['queue', 'setup'],
]
const
const index: TermIndex
index
=
termIndex<string[]>(records: Iterable<string[]>, termsOf: (record: string[], id: number) => readonly string[]): TermIndex

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.

termIndex
(
const docs: string[][]
docs
, (
tokens: string[]
tokens
) =>
tokens: string[]
tokens
)
const
const vocabulary: PrefixIndex
vocabulary
=
function prefixIndex(input: Iterable<string>): PrefixIndex

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.

prefixIndex
(
const index: TermIndex
index
.
TermIndex.keys: () => IterableIterator<string>

Iterate distinct terms in first-seen order.

keys
())
const
const terms: string[]
terms
=
var Array: ArrayConstructor
Array
.
ArrayConstructor.from<string>(iterable: Iterable<string> | ArrayLike<string>): string[] (+3 overloads)

Creates an array from an iterable object.

@param ― iterable An iterable object to convert to an array.

from
(
const vocabulary: PrefixIndex
vocabulary
.
PrefixIndex.keysWithPrefix: (prefix: string, limit: number) => IterableIterator<string>

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.

keysWithPrefix
('cac', 8))
const
const hits: readonly TermHit[]
hits
=
const index: TermIndex
index
.
TermIndex.search: (terms: readonly string[], options?: TermSearchOptions) => readonly TermHit[]

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
(
const terms: string[]
terms
, {
TermSearchOptions.limit?: number | undefined

Maximum number of hits. Defaults to 10. Zero yields no hits.

limit
: 5 })
  • fetch up to a fixed number of autocomplete candidates from a static vocabulary
  • expand the last query token into matching terms before an exact posting query
  • list identifiers or paths starting with a string without scanning all keys
{"v":1,"box":[55.3681,94.1597,658.9333,437.3555],"x":{"s":"linear","lim":[0.325,16.675],"inv":false,"g":0,"t":"Maximum terms to expand (all 16 start with “con”)","tk":[[1,"1"],[4,"4"],[8,"8"],[12,"12"],[16,"16"]]},"y":{"s":"linear","lim":[-0.145833333333,104.3125],"inv":false,"g":0,"tk":[[0,"0"],[25,"25"],[50,"50"],[75,"75"],[100,"100"]]},"ti":"A cap of 8 terms reaches half the “con” matches; frequency order would need 4","a":[{"k":"line","c":"#fffff8","s":"x","d":[1,12.5,2,25,3,37.5,4,50,5,54.1666666667,6,58.3333333333,7,62.5,8,66.6666666667,9,70.8333333333,10,75,11,79.1666666667,12,83.3333333333,13,87.5,14,91.6666666667,15,95.8333333333,16,100],"p":[80.29,395.81,117.2,354.74,154.12,313.67,191.03,272.6,227.95,258.91,264.86,245.22,301.78,231.53,338.69,217.84,375.61,204.15,412.52,190.47,449.44,176.78,486.35,163.09,523.27,149.4,560.18,135.71,597.1,122.02,634.02,108.33]},{"k":"line","c":"#5b6b7c","s":"x","d":[1,12.5,2,25,3,37.5,4,50,5,54.1666666667,6,58.3333333333,7,62.5,8,66.6666666667,9,70.8333333333,10,75,11,79.1666666667,12,83.3333333333,13,87.5,14,91.6666666667,15,95.8333333333,16,100],"p":[80.29,395.81,117.2,354.74,154.12,313.67,191.03,272.6,227.95,258.91,264.86,245.22,301.78,231.53,338.69,217.84,375.61,204.15,412.52,190.47,449.44,176.78,486.35,163.09,523.27,149.4,560.18,135.71,597.1,122.02,634.02,108.33]},{"k":"line","c":"#fffff8","s":"x","d":[1,4.16666666667,2,8.33333333333,3,20.8333333333,4,25,5,29.1666666667,6,41.6666666667,7,45.8333333333,8,50,9,54.1666666667,10,58.3333333333,11,62.5,12,75,13,79.1666666667,14,91.6666666667,15,95.8333333333,16,100],"p":[80.29,423.19,117.2,409.5,154.12,368.43,191.03,354.74,227.95,341.05,264.86,299.98,301.78,286.29,338.69,272.6,375.61,258.91,412.52,245.22,449.44,231.53,486.35,190.47,523.27,176.78,560.18,135.71,597.1,122.02,634.02,108.33]},{"k":"line","c":"#1d5fa6","s":"x","d":[1,4.16666666667,2,8.33333333333,3,20.8333333333,4,25,5,29.1666666667,6,41.6666666667,7,45.8333333333,8,50,9,54.1666666667,10,58.3333333333,11,62.5,12,75,13,79.1666666667,14,91.6666666667,15,95.8333333333,16,100],"p":[80.29,423.19,117.2,409.5,154.12,368.43,191.03,354.74,227.95,341.05,264.86,299.98,301.78,286.29,338.69,272.6,375.61,258.91,412.52,245.22,449.44,231.53,486.35,190.47,523.27,176.78,560.18,135.71,597.1,122.02,634.02,108.33]},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"line","c":"#8c8c84"},{"k":"annotation","c":"#5c5c57"},{"k":"annotation","c":"#5c5c57"},{"k":"annotation","c":"#5b6b7c"},{"k":"annotation","c":"#1d5fa6"}],"lg":[]} 1 4 8 12 16 Maximum terms to expand (all 16 start with “con”) 0 25 50 75 100 half the matches Share of the 768 articles a full scan finds (%) Most frequent terms first Alphabetical, as keysWithPrefix yields A cap of 8 terms reaches half the “con” matches; frequency order would need 4
{"v":1,"box":[55.3681,122.3921,210.9333,437.3555],"x":{"s":"linear","lim":[0.325,16.675],"inv":false,"g":0,"t":"Maximum terms to expand (all 16 start with “con”)","tk":[[1,"1"],[4,"4"],[8,"8"],[12,"12"],[16,"16"]]},"y":{"s":"linear","lim":[-0.145833333333,104.3125],"inv":false,"g":0,"tk":[[0,"0"],[25,"25"],[50,"50"],[75,"75"],[100,"100"]]},"ti":"A cap of 8 terms reaches half the “con”\nmatches; frequency order would need 4","a":[{"k":"line","c":"#fffff8","s":"x","d":[1,12.5,2,25,3,37.5,4,50,5,54.1666666667,6,58.3333333333,7,62.5,8,66.6666666667,9,70.8333333333,10,75,11,79.1666666667,12,83.3333333333,13,87.5,14,91.6666666667,15,95.8333333333,16,100],"p":[61.79,399.23,71.31,361.54,80.82,323.85,90.33,286.16,99.85,273.59,109.36,261.03,118.88,248.47,128.39,235.9,137.91,223.34,147.42,210.78,156.94,198.21,166.45,185.65,175.97,173.09,185.48,160.52,195,147.96,204.51,135.4]},{"k":"line","c":"#5b6b7c","s":"x","d":[1,12.5,2,25,3,37.5,4,50,5,54.1666666667,6,58.3333333333,7,62.5,8,66.6666666667,9,70.8333333333,10,75,11,79.1666666667,12,83.3333333333,13,87.5,14,91.6666666667,15,95.8333333333,16,100],"p":[61.79,399.23,71.31,361.54,80.82,323.85,90.33,286.16,99.85,273.59,109.36,261.03,118.88,248.47,128.39,235.9,137.91,223.34,147.42,210.78,156.94,198.21,166.45,185.65,175.97,173.09,185.48,160.52,195,147.96,204.51,135.4]},{"k":"line","c":"#fffff8","s":"x","d":[1,4.16666666667,2,8.33333333333,3,20.8333333333,4,25,5,29.1666666667,6,41.6666666667,7,45.8333333333,8,50,9,54.1666666667,10,58.3333333333,11,62.5,12,75,13,79.1666666667,14,91.6666666667,15,95.8333333333,16,100],"p":[61.79,424.35,71.31,411.79,80.82,374.1,90.33,361.54,99.85,348.97,109.36,311.28,118.88,298.72,128.39,286.16,137.91,273.59,147.42,261.03,156.94,248.47,166.45,210.78,175.97,198.21,185.48,160.52,195,147.96,204.51,135.4]},{"k":"line","c":"#1d5fa6","s":"x","d":[1,4.16666666667,2,8.33333333333,3,20.8333333333,4,25,5,29.1666666667,6,41.6666666667,7,45.8333333333,8,50,9,54.1666666667,10,58.3333333333,11,62.5,12,75,13,79.1666666667,14,91.6666666667,15,95.8333333333,16,100],"p":[61.79,424.35,71.31,411.79,80.82,374.1,90.33,361.54,99.85,348.97,109.36,311.28,118.88,298.72,128.39,286.16,137.91,273.59,147.42,261.03,156.94,248.47,166.45,210.78,175.97,198.21,185.48,160.52,195,147.96,204.51,135.4]},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"line","c":"#8c8c84"},{"k":"annotation","c":"#5c5c57"},{"k":"annotation","c":"#5c5c57"},{"k":"annotation","c":"#5b6b7c"},{"k":"annotation","c":"#1d5fa6"}],"lg":[]} 1 4 8 12 16 Maximum terms to expand (all 16 start with “con”) 0 25 50 75 100 half the matches Share of the 768 articles a full scan finds (%) Most frequent terms first Alphabetical, as keysWithPrefix yields A cap of 8 terms reaches half the “con” matches; frequency order would need 4
  • 2,048 generated help titles.
  • 16 terms start with “con”.
  • Full scan → 768 matching articles.
  • Term cap 1 → 32 articles (4.2% of full scan)
  • Suggestions → concurrency
  • Example → Concurrency reference on Linux
  • Term cap 4 → 192 articles (25% of full scan)
  • Suggestions → concurrency, config, configuration, configure
  • Example → Configure reference on Linux
  • Term cap 8 → 384 articles (50% of full scan)
  • Suggestions → concurrency, config, …, console
  • Example → Console reference on Linux

Hover or focus a chart for exact values. Drawn by demo.ts against knotkit/prefix-index.

ExportReturnsDoes
prefixIndexPrefixIndexSnapshot distinct strings and enumerate at most the requested number of keys beginning with a prefix.

Cost per member. Open a row for every operation.

prefixIndex 4 members
MemberCost
buildO(n) expected set operations for n input keys, plus O(u log(u + 1)) comparisons for u distinct keys
sizeO(1)
keysO(1) per key
keysWithPrefixO(log(u + 1) + k) string comparisons for k returned keys, with O(1) extra space
InputTypeGet one from
string keys to indexIterable<string>the caller

Every error this module throws is PrefixIndexInvalidInputError, with a code from a closed, exported list. Branch on the code; the message is for people.

INVALID_LIMIT
If you need to…Use instead
find a stored delimiter-separated key that prefixes the queryknotkit/trie
update keys between ordered range queriesknotkit/ordered-map
rank documents after expanding the queryknotkit/term-index

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.

Prompt · knotkit/prefix-index
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/