Save and Load
Persist an ZBSearch database and restore it without freezing the main thread.
ZBSearch databases can be serialized with save and restored with load, so an index can be
built once (at build time, on a server) and shipped to clients ready to search.
Saving and loading
import { create, save, load } from "zbsearch";
const raw = save(db);
const serialized = JSON.stringify(raw);
// later, somewhere else
const db = create({ schema });
load(db, JSON.parse(serialized));The schema you pass to create must match the one the index was built with.
The problem with large indexes
Both steps of the restore path are synchronous and indivisible. For a 100,000-document index,
JSON.parse followed by load holds the main thread for its entire duration, so the page is
frozen until it finishes. Nothing else - rendering, clicks, timers - gets a turn.
The chunked format
save can emit the index as many independently parseable pieces instead of one blob:
import { save, stringifyChunked } from "zbsearch";
const chunked = save(db, { format: "chunked" });
// => { version: 2, chunks: string[] }
const text = stringifyChunked(chunked);stringifyChunked joins the chunks with newlines, which is safe because JSON.stringify never
emits a raw newline. Restore it with parseChunked and loadAsync:
import { create, parseChunked, loadAsync } from "zbsearch";
const db = create({ schema });
await loadAsync(db, parseChunked(text));parseChunked only splits the text - it does not parse it. loadAsync then parses one chunk at a
time, yielding to the event loop between them, and does the same across the stores and index
properties it rebuilds.
Do not JSON.stringify the chunked object
JSON.stringify(save(db, { format: "chunked" })) produces a single blob whose reload is one
monolithic JSON.parse again - the exact cost the format exists to avoid. Persist the chunks as
separate records, or use stringifyChunked / parseChunked.
Chunk size
chunkSize sets the target size in characters for each chunk, defaulting to 524288 (512 KB):
const chunked = save(db, { format: "chunked", chunkSize: 256 * 1024 });Smaller chunks shorten the longest block but produce more of them. A value that cannot be split - a single property's tree - is emitted whole even if it exceeds the target. Sizes are estimated by walking the data, so large objects are split without ever being serialized whole; the chunks themselves are still strings, so do not join them back into one (use the binary snapshot format for very large indexes).
What it buys you
Restoring a 100,000-document index (204 MB serialized, Node 24):
| Path | Total | Longest block |
|---|---|---|
JSON.parse + load | 562ms | the entire run - nothing else could run |
parseChunked + loadAsync | 710ms | 104ms |
The parse itself is well bounded - measured on its own it never blocks for more than ~10ms at the
default chunk size. The residual 104ms is two things the format cannot address: garbage collection
from the roughly doubled peak memory while both the text and the rebuilt structures are live, and
one indivisible RadixTree.fromJSON call for the largest text property (~33ms at this size).
Total time is around 25% higher, which is the cost of yielding. This trade favours responsiveness over throughput; if you only care about total time, keep using the default format.
The binary snapshot format
Both JSON formats build the serialized index out of JavaScript strings. Past a few tens of thousands of passage-sized documents, that runs into V8's maximum string length, and the strings alone take several times the memory of the index. For large indexes - and for anything with vectors - use the binary snapshot format instead:
import { saveBinary } from "zbsearch";
for await (const segment of saveBinary(db)) {
// each segment is a Uint8Array: write it to a file, IndexedDB, OPFS, R2...
}saveBinary returns an async iterable of Uint8Array segments. Only about one segment is in memory
at a time, and it yields to the event loop between segments, so you can write each one out as it
arrives. Postings and term frequencies are delta- and varint-encoded, vectors are written as raw
Float32Array data, and the documents are stored separately from the index. On a synthetic corpus of
passages with 384-dimension vectors the snapshot is about 3.6x smaller than the chunked JSON format.
Restore it with loadBinary, which accepts a Uint8Array, an ArrayBuffer, a ReadableStream, or
any sync or async iterable of byte chunks - split anywhere, not necessarily on segment boundaries:
import { create, loadBinary } from "zbsearch";
import { createReadStream } from "node:fs";
const db = create({ schema });
await loadBinary(db, createReadStream("./index.zbsb"));In the browser, await loadBinary(db, (await fileHandle.getFile()).stream()) reads straight from
OPFS, and await loadBinary(db, response.body) from a fetch response.
loadBinary yields to the event loop as it goes, and only modifies the database once the whole
snapshot has been read and verified, so a failed load leaves it untouched.
Segment size
segmentSize sets the payload size of each segment in bytes, defaulting to 1048576 (1 MiB):
for await (const segment of saveBinary(db, { segmentSize: 256 * 1024 })) {
// ...
}Every segment except the last carries exactly segmentSize payload bytes plus a 12-byte header (the
first one also starts with the 16-byte file header). One exception to the memory bound: each vector
index is copied into flat typed arrays (n x dimensions x 4 bytes for float32 vectors) while it is
being written.
Integrity and versioning
The snapshot starts with a magic number and a format version, and every segment carries a sequence
number and a CRC32 checksum. loadBinary rejects, with an error that has a code:
- a corrupted segment or header:
BINARY_SNAPSHOT_CHECKSUM_MISMATCH; - a snapshot written by an incompatible format version:
UNSUPPORTED_BINARY_SNAPSHOT_VERSION; - data that is not a snapshot, or a truncated, reordered or otherwise malformed one:
INVALID_BINARY_SNAPSHOT.
Checksums are verified by default; pass { verifyChecksums: false } as the third argument to skip
them. isBinarySnapshot(bytes) tells a snapshot apart from other payloads by its magic number.
Synchronous variants
For small indexes, saveBinarySync(db) returns the whole snapshot as one Uint8Array and
loadBinarySync(db, bytes) restores it synchronously. They block, and hold the whole snapshot in
memory, so prefer the streaming functions for anything large.
Vector indexes
Vector indexes are written through toBinarySnapshot(), which returns a small JSON-serializable
meta (including the index kind) and a set of typed arrays. The factory restores them with
fromBinarySnapshot(snapshot). Every built-in index implements both - flat, IVF and their
quantized variants, whose int8 or binary codes are written as
raw bytes - and a custom vector index without them is written through toJSON instead. When loading, the database must be created with a
vector index factory of the same kind as the one that was saved (for example ivf() for an IVF
index).
What it buys you
Synthetic passages (150-250 words from a 40,000-word Zipf vocabulary, seven fields and a 384-dimension vector), Node 22, saved to and loaded from a file:
| Passages | Chunked JSON | Binary snapshot | Save (JSON / binary) | Load (loadAsync / loadBinary) |
|---|---|---|---|---|
| 10,000 | 130 MB | 36 MB | 1.5 s / 0.6 s | 1.2 s / 0.6 s |
| 20,000 | 258 MB | 72 MB | 2.9 s / 1.4 s | 2.2 s / 1.2 s |
| 50,000 | 639 MB | 178 MB | 8.5 s / 4.6 s | 6.9 s / 3.6 s |
| 100,000 | - | 356 MB | - / 9.3 s | - / 6.9 s |
At 50,000 passages the chunked JSON chunks alone held 1.3 GB of memory while saving; saveBinary held
about 125 MB at its peak, most of it the flat copy of the vector index. With store: { vectors: true }
(vectors also kept in the documents), the snapshot is 3.2x smaller than the chunked JSON.
The custom components you pass to create are written through their own save output with a
generic binary encoding, and restored through their load.
Staying on the default format
The chunked format is entirely opt-in. save(db) returns exactly what it always has, and you can
pass the format explicitly when it comes from configuration:
const raw = save(db, { format: "default" });loadAsync accepts both formats, so you can adopt the yielding restore without changing how the
index is stored:
await loadAsync(db, JSON.parse(serialized));This still yields between stores and index properties, but the JSON.parse remains one block.
Compatibility
- Indexes saved by older versions load unchanged through
loadandloadAsync. Loading does not re-tokenize the documents, so an index carries the tokens of the version that built it. When a release changes how text is split (for example, hyphens and apostrophes at the edges of a word are now trimmed, and letters from any script are kept), searches and removals on an old index use the new rules against the old tokens. Rebuild and re-save the index after upgrading. loadalso accepts the chunked format, so you are not forced into the async API.- The
storeconfiguration is saved with the index (store, plusindex.unstoredValuesandindex.unstoredTermsfor removing values that are not in the documents store), andloadrestores it in place of the one given tocreate. Indexes saved by 4.0.x have none: the configuration given tocreateapplies to new documents, and the vectors those indexes stored in their documents are kept and returned as they were. - A chunked index cannot be read by a version of ZBSearch that predates the format. The
envelope carries a
versionfield and a mismatched reader throws rather than misbehaving, but older releases have no knowledge of it at all. Keep publishing the default format until every consumer is upgraded. - Binary snapshots carry the
storeconfiguration and the unstored index state too, andloadBinaryrestores them the same wayloaddoes. They cannot be read by versions of ZBSearch that predatesaveBinary; a reader that does not know the snapshot's format version rejects it withUNSUPPORTED_BINARY_SNAPSHOT_VERSION. - A database with frozen segments is written as binary format version 2,
which ZBSearch 4.0.x rejects with
UNSUPPORTED_BINARY_SNAPSHOT_VERSION; any other database is still written as version 1. Both versions load.saveadds afrozenkey to the index only when there are frozen segments.