@calcit/finger-vec

中文文档
0.1.1发布于 2026/10/02MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 1.3万

Persistent vector for the Calcit runtime: chunked relaxed B-tree with front and back buffers

原始使用文档
文档目录6 节
  1. FingerVec (TypeScript)
  2. Layout
  3. Usage
  4. Performance
  5. Development
  6. License

FingerVec (TypeScript)

A persistent (immutable, structurally shared) vector built for the Calcit JavaScript runtime. It replaces the list part of @calcit/ternary-tree. The Rust twin lives in finger-vec.rs; both use the same layout and algorithms.

npm

Layout

front buffer | relaxed B-tree of chunks | back buffer
  • Elements live in chunks of up to 32 items; lists of 32 items or fewer are one array.
  • The middle is a relaxed B-tree (branching factor 32, cumulative sizes, all leaves at the same depth), as in RRB vectors.
  • Buffers at both ends make pushes and pops cheap. Appending to the newest version of a list writes into its own back array instead of copying; older versions never read past their own end, so they are unaffected.
  • Concatenation joins along the seam and merges the nodes there, so concatenating in a loop keeps the height logarithmic.
OperationCost
get, assocO(log₃₂ n)
pushLeft, pushRight, dropLeft, dropRightamortized O(1) plus a chunk copy
concat, split, slice, insert, dissocO(log n)
FingerVec.from(array)O(n)

Usage

import { FingerVec } from "@calcit/finger-vec";

const xs = FingerVec.of(1, 2, 3);
const ys = xs.pushRight(4).pushLeft(0);
ys.toArray(); // [0, 1, 2, 3, 4]
xs.len(); // 3, unchanged

const [left, right] = ys.split(2);
left.concat(right).equals(ys); // true
ys.slice(1, 3).toArray(); // [1, 2]
for (const x of ys) console.log(x);

Main methods: len, get, first, last, pushLeft/prepend, pushRight/append, dropLeft, dropRight, rest, butlast, assoc, dissoc, insert(idx, item, after), insertAt, assocBefore, assocAfter, split, slice, take, skip, concat, FingerVec.concatAll, reverse, map, forEach, findIndex, indexOf, toArray, equals, items() and for...of. get returns undefined out of range; rest, butlast, assoc, dissoc, insert and slice throw on invalid input, matching the ternary-tree functions Calcit used.

Performance

Measured against @calcit/ternary-tree 0.0.26 on Node 22, Linux x86-64, single runs:

Caseternary-treeFingerVecSpeedup
append 100k70.00 ms24.87 ms2.8×
prepend 100k82.35 ms48.84 ms1.7×
from array 100k19.46 ms0.95 ms20×
random get 100k58.10 ms20.59 ms2.8×
rest until empty, 100k32.91 ms18.94 ms1.7×
random assoc 100k148.21 ms90.48 ms1.6×
concat acc [x] 5k, then get every item118.18 ms1.65 ms72×
insert in the middle 3k174.84 ms32.33 ms5.4×
queue: append + rest 100k110.77 ms22.44 ms4.9×
32-item list: from array ×300k543.96 ms30.94 ms18×
8-item list: append to the same base ×300k24.65 ms48.42 ms0.5×
concat acc [x] 5k6.33 ms9.41 ms0.7×

Appending repeatedly to one shared small list copies its chunk each time, which is slower than ternary-tree's small nodes. The ternary tree, on the other hand, reaches depth 2,501 after 5,000 concatenations and overflows the stack after a few thousand middle insertions; FingerVec stays at depth 3–4. Run npm run bench for FingerVec numbers on your machine.

Development

打包和发布时会自动执行 TypeScript 构建。运行 npm run test:package 会检查真实 tarball 的文件列表,并在独立临时项目中验证公开类型与 Node 导入;测试不依赖仓库内的源码或本地模块链接。

发布通过 .github/workflows/npm-publish.yaml 使用 npm OIDC trusted publishing,不依赖发布 token。 npm 的 trusted publisher 配置须指定 calcit-lang/finger-vec.ts 和文件名 npm-publish.yaml;当前 job 没有 GitHub Environment,该选项留空。保存配置后可通过 Release 或手动运行同一 workflow 发布;2FA 保持启用。

npm install
npx tsc
node lib/test.mjs
node lib/bench.mjs

Tests compare random operation sequences against arrays, check structural invariants after every step, and assert depth bounds for concatenation loops and middle insertion.

License

MIT

版本与发布记录

@calcit/finger-vec 共发布 2 个版本,首次发布于 2026/10/02;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.1.1latest2026/10/021 个月前
0.1.02026/10/021 个月前

关于 @calcit/finger-vec 的常见问题

@calcit/finger-vec 是什么?

Persistent vector for the Calcit runtime: chunked relaxed B-tree with front and back buffers

如何安装 @calcit/finger-vec?

在项目目录运行 npm install @calcit/finger-vec,或使用 pnpm add / yarn add / bun add。

@calcit/finger-vec 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@calcit/finger-vec 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@calcit/finger-vec 可以商用吗?

@calcit/finger-vec 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@calcit/finger-vec 的下载量有多少?

根据 npm 官方下载统计,@calcit/finger-vec 在 2026/10/09 – 2026/11/07 这 30 天内被下载 13,414 次,日均约 447 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@calcit/finger-vec 还在维护吗?

最新版本 0.1.1 发布于 2026/10/02(1 个月前);至今共发布 2 个版本,其中过去 12 个月发布了 2 个;首次发布于 2026/10/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。