Lightweight, fast in-memory B+Tree in TypeScript. Generic over TKey/TEntry; behaves as an ordered set (key = entry) or sorted dictionary (key extracted from entry). See readme.md for usage.
src/b-tree.ts—BTree<TKey, TEntry>class; all public API and balancing logic.NodeCapacity = 64(fixed, not configurable).src/nodes.ts—LeafNode(holds entries) andBranchNode(partitions + child nodes). Data lives only in leaves; no leaf linked-list.src/path.ts—Pathcursor (branches,leafNode,leafIndex,on,version) andPathBranch.src/key-range.ts—KeyRangeforrange().src/index.ts— barrel export.test/*.test.ts— mocha + chai.
- Build:
yarn build(ornpm run build) — cleans thentsc -p tsconfig.build.json. - Test:
yarn test(ornpm test) — mocha overtest/**/*.test.tsvia ts-node ESM loader. - Docs:
yarn doc(typedoc). - Package manager is yarn 4; ESM (
"type": "module") — use.jsextensions in imports.
- Paths are versioned cursors. Any mutation bumps
_versionand invalidates all outstanding paths except the one a mutation method returns. Public methods validate the path version and throw on stale paths. OnlymoveNext/movePriormutate a path in place; everything else returns a new one. on= cursor is on an entry vs. in a "crack" between/beyond entries.findreturns the entry or the crack before it;next/priormove to the nearest match from a crack.- Entries are frozen on insert to deter key mutation — but freezing is shallow and non-transitive. Never mutate a key after insert; use
updateAt/upsert/merge/deleteAt. - Sort consistency over correctness. The default compare uses
</>; a customcomparemust be consistent, but the tree does not police Ecmascript comparison quirks. - Public API:
insert,updateAt,deleteAt,upsert,merge,find,get,at,first,last,next/prior,moveNext/movePrior,ascending/descending,range,getCount,isValid. All complexity O(log n);getCountis computed, not stored.
- Follow
.editorconfig: tabs (size 2), UTF-8, single quotes in.ts, final newline. (Markdown uses spaces.) - Stay minimalistic — helper/convenience features belong in an add-on library, not core.
- Performance is workload-sensitive: an "improvement" for one access pattern often regresses another. Benchmark broadly before claiming a speedup; add a failing-without-the-fix test for bug fixes.
This project uses tess for AI-driven ticket management. Read and follow the ticket workflow rules in tess/agent-rules/tickets.md. Tickets are in the tickets/ directory.