Skip to content

About

Gradual, behavior-verified migration to Zig. Record what your JavaScript, TypeScript or Python functions do, port them one at a time, and prove each port matches before callers switch over. CLI and MCP server.

Topics

Resources

Contributing

Stars

9 stars

Watchers

0 watching

Forks

Repository files navigation

zig-anything logo

zig-anything

Latest release CI status Zig 0.17.1 GitHub stars MIT license

Move hot code to Zig one function at a time, with every step checked against the original's recorded behavior.
Functions in JavaScript, TypeScript and Python. Command-line programs in any language.

Documentation: Guide · Architecture · Changelog

A Python Levenshtein function and its Zig port running the same 253 recorded cases: 134.6 ms versus 6.5 ms

20.7× faster
134.6 ms → 6.5 ms
253 / 253
recorded cases identical
6 platforms
Linux, macOS, Windows · x86_64 and arm64

levenshtein from examples/levenshtein-py: Python 3.12 vs its Zig port, wall time for a fresh process running all 253 recorded cases once, median of 5 runs. Ryzen 7 5700X, Windows 11. Methodology and the other results, including slower ones, are in Measured results.

What it is

Rewrites usually fail in the gap between "the new code compiles" and "the new code does what the old code did". zig-anything closes that gap with a behavior-verified migration, not a rewrite:

  1. Record. The original function runs in its own runtime (Node.js or Python) on generated edge cases, semantic traps and your own inputs. Every result, including thrown errors, is stored.
  2. Port. You or your coding agent write the Zig version of that one function.
  3. Verify. Every recorded case is replayed against the port and compared semantically. gate passes only when all of them match.
  4. Integrate. Callers switch to the port through a native bridge, your test suite runs, and the source rolls back automatically if anything fails.

You move one function at a time, hottest first, and the application keeps working after every step.

$ zig-anything diff fuzzyScore
case 22: different value
  args      ["🎉","emoji 🎉 ok"]
  expected  {"ok":0.75}
  got       {"ok":0.7}
285/288 cases match  [zig-port/fuzzyScore]

A real mismatch: the first port counted UTF-8 bytes, but JavaScript counts UTF-16 code units. The recording caught it before any caller did.

Quick start

Paste this into your coding agent:

Install zig-anything with npm i -g https://github.com/Layfate/zig-anything/releases/download/v0.3.0/zig-anything-0.3.0.tgz, then run zig-anything setup-agent. Run zig-anything plan, port each function in that order until zig-anything gate passes for it, then switch callers to it with zig-anything integrate.

Or install it yourself:

npm i -g https://github.com/Layfate/zig-anything/releases/download/v0.3.0/zig-anything-0.3.0.tgz
zig-anything setup-agent   # registers the MCP server with Claude Code, Cursor and VS Code

Each release also ships Python wheels and standalone binaries for Linux, macOS and Windows on x86_64 and arm64. The binary is a single static executable of about 1 MB with no runtime dependencies.

How it works

zig-anything plan src/                        # what to port, in what order
zig-anything init src/text.mjs slugify        # scaffold zig-port/slugify
zig-anything record slugify                   # record what the original does
zig-anything coverage slugify                 # lines no recorded case executes
$EDITOR zig-port/slugify/src/port.zig         # write the port
zig-anything diff slugify                     # repeat until every case matches
zig-anything gate slugify --min-coverage 90   # done means this passes
zig-anything bench slugify                    # measure both sides on the same inputs
zig-anything integrate slugify -- npm test    # switch callers to the port, verified
  • Semantic comparison. 1, 1.0 and 1e0 are equal, object key order is ignored, NaN and ±Infinity survive the round trip, and thrown errors match returned Zig errors.
  • Native bridge. A finished port is callable from Node through a Node-API addon (WebAssembly fallback) and from Python through a CPython extension (ctypes fallback). TypedArrays and numpy arrays pass without copying, and --targets all cross-compiles both for every platform from one machine, so you can use the port before the rest of the migration is done.
  • Coverage you can see. coverage lists the lines of the original that no recorded case executes, so you know where the port is unchecked. gate --min-coverage makes a minimum part of done.
  • Any module style. integrate rewires ES modules, CommonJS, TypeScript and Python, and keeps async functions async.
  • Agent-ready. zig-anything mcp exposes every command as a Model Context Protocol tool.

The guide covers every command, configuration, the bridge, program units and limitations.

Measured results

zig-anything bench <unit> times both sides on the same recorded inputs and reports the result even when the port is slower. Numbers from one machine (Ryzen 7 5700X, 32 GB, Windows 11, Zig 0.17.1, Node.js 24.21, Python 3.12.10), median of 5 runs:

Unit Original Per call One process, all cases
levenshtein (253 cases) Python 3.12 51.1 µs → 0.31 µs (164×) 134.6 ms → 6.5 ms (20.7×)
fuzzyScore (288 cases) Node.js 24, V8 JIT 61.3 ns → 32.5 ns (1.9×) 43.6 ms → 6.9 ms (6.3×)

How to read these:

  • "One process" is wall time for a fresh process that loads the code and runs every case once, so it includes interpreter startup. "Per call" excludes startup and uses calibrated timed passes after a warm-up.
  • Calling a port from the original language costs something. Through the native Python extension, levenshtein goes from 48.2 µs to 0.42 µs per call: 115×, not 164× (the ctypes fallback manages 7×). Through the Node addon, fuzzyScore is slower than calling it in place, because a 70 ns function in a JIT is cheaper than any language crossing. Port whole hot paths, or use the batch entry points.
  • A JIT is a strong baseline. The first fuzzyScore port matched every case but ran 2.6× slower than V8. Profiling found the cause, and the recorded cases stayed green through the fix. The story is in examples/fuzzy-js.

Every number has a reproduce command in the guide's benchmark section. Your numbers will differ.

Why Zig?

zig-anything targets Zig because of what a gradual migration needs: small, self-contained native code that any runtime can call and that builds for every platform from one machine.

Zig Rust Cython
Callable from Node and Python Yes, through the C ABI, Node-API and WebAssembly Yes, through napi-rs, PyO3 and WebAssembly Python (CPython) only
Cross-compiling Built into the compiler, including libc; one machine builds all six release targets Good, but targets with a C toolchain or libc usually need extra setup (cross, zigbuild) One build per platform and Python version, or the stable ABI
Memory safety Runtime checks in Debug and ReleaseSafe builds; no compile-time guarantee Guaranteed at compile time outside unsafe Python's guarantees, until you use C-level types and pointers
Learning curve for a one-function port Small language, explicit allocation, no hidden control flow Ownership and lifetimes take longer to learn Lowest for Python developers: start from the Python code
Stability and ecosystem Pre-1.0: releases contain breaking changes, smaller package ecosystem Stable since 2015, large ecosystem (crates.io) Mature and stable, tied to CPython

Choose Rust when you need compile-time memory safety, are writing a large, long-lived native component, or need a mature library ecosystem. Choose Cython when the code only ever runs inside CPython and you want to speed it up without leaving Python's syntax. zig-anything is built for the case in between: replacing hot functions in JavaScript, TypeScript or Python codebases with small native ports, verified one at a time, that work from either runtime and ship to every platform without per-platform toolchains.

Verification partly offsets the lack of compile-time memory safety. diff and gate build ports in ReleaseSafe mode by default, so bounds violations and integer overflow on any recorded input fail verification instead of reaching production. It is a test, not a proof: inputs that were never recorded are not checked.

Zig version compatibility

Zig has not reached 1.0, and each minor release can break existing code. zig-anything therefore pins one Zig version instead of supporting a range:

zig-anything Zig Status
0.3.x 0.17.1 Current. Tested in CI on Linux, macOS and Windows (x86_64 and arm64)
0.2.x 0.17.1 Previous
  • The pinned version is the contract. The tool, its scaffolded ports, harnesses and bridge code all target the version above. build.zig.zon declares it as minimum_zig_version, and CI builds and tests with exactly that version.
  • Use the pinned version for your ports. zig-anything runs zig from your PATH, or the compiler named by the ZIG environment variable or the "zig" setting in zig-anything.json. Other versions may work but are not tested.
  • Upgrades happen in a minor release. Moving to a new Zig version is a single change that updates the CI matrix, build.zig.zon and the scaffolding that init and bridge generate together, passes the full unit and end-to-end suite on every platform, and is recorded in the changelog with any edits your existing ports need.
  • Your recordings survive upgrades. Recorded cases are plain JSON lines that don't depend on Zig. After upgrading Zig, run zig-anything gate on each unit to confirm every port still matches the original exactly.

Requirements

Needed for Tool
building and verifying ports Zig 0.17.1
recording JavaScript Node.js 22.6+
recording TypeScript Node.js 23.6+ (native type stripping), or a configured loader
recording Python Python 3.8+

Scope

A passing gate means "equal on every recorded case", not "proven equal", so the quality of a port depends on the quality of its inputs. coverage shows which lines of the original no case executes, fuzz searches for inputs the recording missed, and domain-specific inputs belong in inputs.jsonl. Function units take and return JSON-representable values. File, network and database side effects are not compared. The full list is in Scope and limitations.

License

MIT © zig-anything contributors. Ports and generated code in your repository are yours.

About

Gradual, behavior-verified migration to Zig. Record what your JavaScript, TypeScript or Python functions do, port them one at a time, and prove each port matches before callers switch over. CLI and MCP server.

Topics

Resources

Contributing

Stars

9 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages