Repository navigation
Node
In Node, npm install parsec-web gives you Parsec built for your machine, not only WebAssembly. Each platform's build is an optional dependency of the package, so npm fetches the one that fits and skips the rest; the package loads it when it's there and falls back to the WebAssembly when it isn't. It's the same API either way, and code written for one runs on the other.
What you get for it: about 1.5 times the parse speed, and parseAsync, which parses on a worker thread while your code carries on.
On this page: Which build you get · parseAsync · What differs · Speed · How it's built and released · Working on it
| Platform | Build |
|---|---|
| Linux x86-64 and ARM64, glibc | native |
| Linux x86-64 and ARM64, musl (Alpine) | native |
| macOS x86-64 and ARM64 | native |
| Windows x86-64 and ARM64 | native |
| Anywhere else in Node | WebAssembly |
| Browsers and bundlers | WebAssembly |
import { backend } from "parsec-web";
backend; // "native" or "wasm"Set PARSEC_BACKEND=wasm to skip the native build, say to compare the two, or PARSEC_BACKEND=native to fail loudly when it can't load rather than fall back quietly. An install made with --no-optional (or --omit=optional) has no native build, and gets the WebAssembly.
On Linux the package tells glibc from musl by what Node reports about itself (process.report), as napi-rs's own loader does.
import { parseAsync } from "parsec-web";
const doc = await parseAsync(html, { spans: true });It takes what parse takes and resolves to what parse returns. The parse runs on Node's libuv thread pool, so the event loop keeps turning: a server parsing the 15.8 MB HTML spec goes on answering requests for the 70 ms it takes. The pool has four threads unless UV_THREADPOOL_SIZE says otherwise, and file system work shares them.
The string is copied into the addon before the promise comes back, so change or drop yours as you like. With the WebAssembly build, parseAsync parses before it returns and only the result is a promise: there's no thread to hand the work to.
A panic in the native build is an Error, or a rejected promise from parseAsync, as it is in the WebAssembly and Python builds; it never ends the process.
Nothing your code should have to know about, which is the point. The details:
-
Memory. A native document belongs to the garbage collector, as any JavaScript object does. The WebAssembly's advice about freeing documents in long loops (see WASM) doesn't apply.
free()andusingstill work, so the same code runs on both, but on the native build they do nothing. -
Classes.
Document,Node,Span,ParseErrorandByteParserare the native build's own classes, so an object from one build isn't aninstanceofthe other's. Mixing the two in one process only happens if you load both on purpose. -
Errors have the same messages. A native one also has a
code(GenericFailure), which the WebAssembly's don't.
Parsing the 13 pages in bench/, 24 MB in all, in Node 22.14 on an Apple M2 Max: the median of 30 parses of each page as a string, MB/s.
| Page | MB | Native | WebAssembly | Native / WebAssembly |
|---|---|---|---|---|
| bbc-news | 0.40 | 624 | 379 | 1.65 |
| ebay | 0.53 | 495 | 366 | 1.35 |
| github-rust | 0.33 | 389 | 239 | 1.63 |
| hacker-news | 0.04 | 221 | 128 | 1.72 |
| mdn-html | 0.19 | 347 | 222 | 1.57 |
| nytimes | 1.57 | 639 | 498 | 1.28 |
| python-stdtypes | 0.77 | 213 | 112 | 1.90 |
| reddit-programming | 0.58 | 607 | 380 | 1.60 |
| stackoverflow | 1.16 | 381 | 243 | 1.57 |
| whatwg-full | 15.78 | 211 | 141 | 1.50 |
| whatwg-parsing | 0.83 | 242 | 166 | 1.46 |
| wikipedia-html | 0.81 | 317 | 212 | 1.49 |
| wikipedia-population | 1.39 | 385 | 257 | 1.50 |
| all pages | 24.36 | 249 | 165 | 1.51 |
Both builds start from a JavaScript string and pay to turn it into UTF-8 first. On whatwg-full that's about 12 of the 69 ms; parseBytes on a Buffer you already have skips it. To run it yourself: bench/fetch-pages.sh, build as in Working on it, then node bindings/node/bench.mjs bench/corpus.
bindings/node is a napi-rs crate whose src/lib.rs mirrors bindings/wasm/src/lib.rs line for line, with Node-API's attributes in place of wasm-bindgen's. A change to the API goes in both, and the shared test suite (bindings/wasm/tests/package.test.mjs) runs against both, so most drift fails it.
CI's node workflow builds the eight platforms, each on its own runner (Alpine in Docker for musl), and on each runs the package's tests twice: against the native build and against the WebAssembly fallback. Each platform's package is an artifact of the run.
On a v* tag, release-npm.yml publishes the eight @parsec-web/binding-<platform> packages and then parsec-web, whose optionalDependencies name them at the same version. It's trusted publishing with provenance, as for the WebAssembly, and it waits for release.yml's release first. The package names come from one field, napi.packageName in bindings/wasm/npm/package.json.
cd bindings/wasm && ./build.sh
cd ../node && cargo build --release
node npm.mjs platform darwin-arm64 target/release/libparsec_web_node.dylib out
cd ../wasm
mkdir -p node_modules/@parsec-web
ln -s ../pkg node_modules/parsec-web
ln -s ../../../node/out/darwin-arm64 node_modules/@parsec-web/binding-darwin-arm64
PARSEC_BACKEND=native node --test tests/package.test.mjs
PARSEC_BACKEND=wasm node --test tests/package.test.mjsOn Linux the library is libparsec_web_node.so and the platform linux-x64-gnu (or -musl, arm64); on Windows, parsec_web_node.dll and win32-x64-msvc.
Parsec is the parsing foundation for the web, in Rust. It's Apache-2.0, 0.3.0 is out, and these pages describe main. Found something wrong here? Open an issue or ask in Discussions.
Repository · Issues · Discussions · Roadmap · Security policy · Licence