Skip to content

vercel/ms

vercel/ms (5.5k stars, commit 4ff48ce) turns "2 days" into milliseconds and back; most Node projects pull it in through some dependency. Ported end to end with rust-anything 1.5.1; the crate is in demo/real-world/vercel-ms-rs.

Step Result
analyze 1 unit (src/index.ts), ready, readiness 96
bridge --kind napi ms, parse, parseStrict and format bridged, none kept in TS
record --from-tests -- jest 975 cases: generated ones, traps, and 340 calls captured from vercel/ms's 167 Jest tests (the first port used 149 inputs copied from them by hand)
port 200 lines of Rust for 184 of TS, including a FormatIndex that reads options once per batch
diff 784/784 matched on the first build; 975/975 with the captured calls
gate passes (fmt, clippy, tests, error paths, size, behavior)
bench, per call 0.56x over the recorded cases (0.39x at first): parse on a normal string takes 79 ns, level with JS, but edge cases and thrown errors still cost more through the bridge
batch, parse x100,000 JS 11.8 ms, Rust 0.65 ms: 18x; 3.4x counting the one-time handover of the strings (2.1x before strings crossed joined)
batch, format x100,000 JS 2.1 ms, Rust 2.5 ms with the handover: 0.8x (0.2x at first); format(x, { long: true }) 1.2x

What the port had to get right, all caught by recorded cases or traps:

  • /.../i without the u flag folds only ASCII letters, while Rust's regex folds Unicode, so "1 ſ" (long s) and the Kelvin sign would have parsed as seconds. The port matches units by hand, ASCII only.
  • Math.round rounds halves up (-2.5 gives -2); Rust's round goes away from zero.
  • ${n} prints 1e+21, 5e-324 and 0 for -0; the port reproduces JS number-to-string. Its first version also had a tie bug (25.728225708007812 printed as ...813) that ms's integer outputs never reach.
  • str.length counts UTF-16 units, so the 100-character limit counts an emoji as two.

Found in the tool, and fixed: a napi batch method with a structured parameter (format's options) did not compile, since the generated binding took a serde_json::Value that napi cannot convert without its serde-json feature. Crates with batch classes now enable it.

Taken back into the tool from this port:

  • The generated js.rs has js::number_to_string and js::math_round, checked against Node on 20,000 random doubles; the port uses them instead of its own copies. number_to_string takes its digits from serde_json's printer: 5x faster than format! for integers, 3x for fractions.
  • napi batches send string items as one joined string with UTF-16 end offsets, number items as one Float64Array, and return string results joined (index.cjs splits them with slice): 2x faster for strings and 20x for numbers than one JS value per item. new Batch(items) and run() keep their shape.
  • analyze flags Math.round, toFixed and /i regexes without u that mention s or k, with guidance on each.
  • Calls cost less: on napi every sync function goes over the binary wire with no napi arguments (an argument costs about 55 ns, the whole call 22 ns), one JS wrapper per function instead of two, serde_json::Value parameters cross as tagged values of any shape, and errors come back in the wire buffer. parse per call went from 121 to 79 ns.
  • record --from-tests replaces copying test inputs by hand: it records each call the suite makes while the tests run, with the capture code kept out of coverage (ms enforces 100%).

The verdict: ms is a good port and a bad speed target. Correct on every recorded case, faster only when a program parses many durations in one go.

Edit this page on GitHub →