Skip to content

Conventions

Every module follows the same rules. Learn them once and the rest of the library reads the same way.

Every structure is created by a function and returned as a typed interface. There are no classes to instantiate or extend.

import { minBinaryHeap, numberOrder } from 'knotkit/heap'
const heap = minBinaryHeap(numberOrder, [5, 8, 3])
heap.push(4)
heap.pop() // 3

The interface is the whole contract, so a structure is an ordinary value: pass it to a function, a test, or a benchmark. Every member is a readonly property, and destructuring a member keeps it working.

When a module ships more than one implementation of the same interface, each one has its own factory and a kind string. roaringBitset() and denseBitset() both return a Bitset; their kind is 'roaring' or 'dense'.

Each module page has a Choose a layout table. Every row is a rule you can check against your workload, such as “you can name the largest id, and it fits in 64 MiB of bitmap”, followed by what that layout costs.

import { DISJOINT_SET_KINDS, __GROWABLE_DISJOINT_SET_LAYOUTS } from 'knotkit/disjoint-set'
DISJOINT_SET_KINDS // ['indexed', 'object', 'struct-of-arrays']
// Run the same test against every layout:
for (const [kind, create] of Object.entries(__GROWABLE_DISJOINT_SET_LAYOUTS)) {
// …
}

Branch on kind, not on instanceof. A kind survives serialization, iframes, and duplicate copies of the package in one bundle; instanceof does not.

Every error is a subclass of KnotkitError with a code taken from a list the module exports, such as HEAP_ERROR_CODES. Each module’s errors fall into categories, such as invalid input (HeapInvalidInputError) or a failure to read saved data (HeapParseError).

import { isInvalidInputError } from 'knotkit/errors'
import { splitmix32, uniformInt } from 'knotkit/prng'
try {
uniformInt(splitmix32(42), 0)
} catch (error) {
if (isInvalidInputError(error) && error.moduleName === 'knotkit/prng') {
error.code // 'INVALID_BOUND'
}
}

Because the lists are exported as as const arrays, a switch over error.code can be exhaustive, and TypeScript reports a missing case when a release adds a code.

Randomness, hashing, and ordering are arguments

Section titled “Randomness, hashing, and ordering are arguments”

No module seeds itself, reads the clock, or picks a hash function for you. You pass them in:

import { splitmix32 } from 'knotkit/prng'
import { shuffled } from 'knotkit/sampling'
const order = shuffled(splitmix32(42), [1, 2, 3, 4, 5]) // the same order on every machine

Record the seed and anyone can reproduce your result: a simulation, a sampled test, or a benchmark.

Modules that can be saved expose serialize* and deserialize* from a /wire subpath, such as knotkit/bitset/wire. Code that never saves a structure never loads the parser.

Saved data is versioned and checked when it is read back. A truncated or tampered artifact fails with a parse error code, not a structure in a broken state.

Module pages list the time cost of each member where the module states one, with the conditions it depends on: O(log c) bitmap container, O(1) amortized. Measured speed-ups name their baseline and link the benchmark that produced them. See Measured results.