I wrote the same CKB lock script twice, once in Rust and once in TypeScript for ckb-js-vm, and ran both through the same 58 scenarios in the real CKB-VM. They agree on every accept/reject decision. The cost is very different, and most of the difference is not where I expected.
The script is the session lock from ckb-session-kit. It lets a browser-held session key spend a bounded amount (per-transaction limit, optional single recipient, optional rate limit, no splitting of session cells, no type scripts the key could use to freeze funds), while the owner can always sweep. It also has a token mode that bounds xUDT amounts the same way. It delegates signature checks to the standard locks, so it does no cryptography. That makes it a test of the VM rather than of a crypto library.
Results (the lock’s own script group only):
Rust ckb-js-vm
Code deployed 7,656 B 30,917 B bytecode + 410,248 B VM
Mean cycles, accepted CKB spends 14,758 15,037,968
Mean cycles, accepted token spends 20,348 15,518,698
Median ratio, all 58 scenarios 1x 1009x
Cheapest run (args rejected) 3,145 14,272,805
What surprised me: the cheapest ckb-js-vm run, which rejects the args almost immediately, still costs 14.3M cycles. That is starting the JavaScript VM and loading the bytecode. An accepted spend costs 15.04M, so the lock’s own logic is about 0.77M cycles, around 5% of the total. The logic runs at roughly 66x the Rust logic’s cycles, but for a script this small the fixed start-up cost dominates.
For scale, a standard secp256k1 lock costs about 1.6M cycles. So the Rust session lock adds under 1% to a typical spend, and the ckb-js-vm version adds about 9x a signature check.
The lock gained three rules and a token mode after my first measurement on 8 Oct, so I re-ran the original 20 scenarios against both versions. The Rust lock’s accepted spends went from 12,263 to 15,393 cycles (+26%: a bigger binary and checks on every output). The ckb-js-vm version went from 14.57M to 15.07M (+3%), and most of that was start-up again. The 8 Oct TypeScript port, rebuilt today, still costs exactly its 8 Oct cycles, so the extra 0.36M comes from loading 1.8 KB more bytecode.
What I take from it:
- For small, frequently run scripts like locks, ckb-js-vm’s start-up cost is the number to know. It does not shrink with better JavaScript, and it grows with bytecode size.
- For larger or rarely run scripts, or for prototyping, ckb-js-vm is a real option. The TypeScript version was much faster to write, and the logic overhead (66x) is smaller than the headline ratio (about 1000x) suggests.
- Writing it twice doubles as a test. Two independent implementations must agree on all 58 scenarios, in CI. Porting the new rules to TypeScript was a useful second read of the Rust code.
How it is measured: ckb-cycle-tools runs each scenario in ckb-debugger through ckb-testtool, one run per script group. CI rebuilds both implementations from source, the Rust lock from a pinned session-kit commit, and re-runs every scenario, so the numbers are reproducible from a clean checkout. Five of the ckb-js-vm scenarios are also in a cross-version check: every ckb-debugger release from 0.121.0 to 1.1.1, native and WebAssembly (14 builds), gave the same cycles and the same result.
The same tool works on your own contracts. Since ckb-cycle-tools 1.1.0 (npm, 31 Oct), a project made from ckb-script-templates can use its existing ckb-testtool tests as the scenarios: a helper call saves each test’s transaction, ckb-cycles scan writes the config, and ckb-cycles report gives cycles per script group and the costliest functions. Steps are in the README.
- Benchmark and full results: ckb-cycle-tools/benchmarks/session-lock/RESULTS.md at main · luongthanhtung03/ckb-cycle-tools · GitHub
- Session lock (Rust): GitHub - luongthanhtung03/ckb-session-kit · GitHub
- Cross-version check: ckb-cycle-tools/compat/RESULTS.md at main · luongthanhtung03/ckb-cycle-tools · GitHub
- ckb-testtool / ckb-script-templates workflow: GitHub - luongthanhtung03/ckb-cycle-tools · GitHub
- npm: https://www.npmjs.com/package/ckb-cycle-tools
A question for people who run ckb-js-vm in production: is there a way to share VM start-up across script groups in one transaction, or is a preloaded VM snapshot planned?