Skip to content

breaking: new wire format, and reducer and reviver changes - #246

Draft
elliott-with-the-longest-name-on-github wants to merge 20 commits into
mainfrom
elliott/new-wire-format
Draft

elliott-with-the-longest-name-on-github wants to merge 20 commits into
mainfrom
elliott/new-wire-format

Conversation

@elliott-with-the-longest-name-on-github

@elliott-with-the-longest-name-on-github elliott-with-the-longest-name-on-github commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

First of two stacked PRs for the next major version. It also includes the reducer and reviver changes that were #247. The full wire format is specified in the new FORMAT.md.

What changes

  • New wire format (tree-format): the payload is plain JSON. Plain data is written as itself, and other values are $-marked strings ("$D1700000000000", "$n123", "$R4", ...). A root that contains references is wrapped as ["$G", value], and every tagged type is ["$:Name", payload], with exactly one payload. Payloads written by earlier versions can no longer be parsed.
  • Built-in reviver payloads (builtin-reviver-payloads): a reviver named after a built-in type gets the whole payload, for example every entry of a Map.
  • RegExp reviver (regexp-reviver-flags): gets [source, flags] instead of only the source. breaking: drop sparse arrays; regular expressions are opt-in #248 removes this changeset, because RegExp stops being a built-in type there.
  • Date reviver (date-reviver-time): gets the time in milliseconds instead of an ISO string.
  • Date operations (date-operations): toISOString/fromISOString are replaced by timeOf/fromTime.
  • Properties may be read more than once (reread-properties). The writer's fast path depends on this.
  • Symbol keys are skipped by stringify/stringifyAsync, as JSON.stringify skips them (symbol-keys). uneval still throws.
  • Promises stay promises (async-promises): stringifyAsync writes ["$A", root, id, result, ...], and parse gives back a promise wherever the value had one. Results are written in the order the promises settle.
  • stringifyAsync fails fast (async-fail-fast): it rejects with the first failure, without waiting for promises earlier in the value.
  • references: false (references-option): a new option on parse, unflatten, stringify and stringifyAsync that rules out repeated and cyclical references.

Reducers and revivers

  • Reducers only see objects and functions (reducers-objects-only). Strings, numbers, booleans, BigInts, null and undefined are always written as themselves, so they stay on the fast path even when reducers are passed. Functions are tracked by identity, so a reducer that returns its own input is an ordinary cycle. This removes the REDUCER_LOOP error and its bookkeeping.

  • Revivers are called once, with the whole payload (two-stage-revivers). A custom value whose payload refers back to the value itself now throws Invalid circular reference. For types that can contain themselves, a reviver can be { create, fill }:

    • create makes the value before its payload is read;
    • fill(value, payload) completes it.

    The two-stage form also works for revivers named after built-ins. Adds the Reviver and TwoStageReviver types.

  • Values still being read hold a marker in their slot. This replaces a separate Set of unrevived values, and makes parsing payloads with custom values about 10% faster. A box or typed array that refers to itself from inside its payload now throws Invalid circular reference too, like a custom value.

  • Refactor: in parse.js, shadow(name, fallback) returns the built-in's own reader when there is no user reviver, which removes a conditional at each call site.

Benchmarks

This PR also brings in #235 by @teemingc, which makes pnpm bench run again and adds uneval benchmarks. On top of that it adds a parse suite, which parses what the same version's stringify writes, and benchmarks for records that share objects, custom types, and references to custom types (benchmarking/shapes.js).

Both columns run exactly the same benchmark files, against main's src/ and this PR's. Times are per call, in microseconds: each benchmark's total time divided by its number of iterations, taking the best of 3 alternating runs of the whole suite on Node 24.19 (Apple Silicon). Lower is better.

parse

Benchmark main (µs per call) This PR (µs per call) Change
small object 0.36 0.28 -23%
100 records 82.9 53.7 -35%
1000 records 815 536 -34%
1000 records, shared authors 680 491 -28%
unique short strings 54.5 39.6 -27%
repeated long strings 31.0 22.1 -29%
string Map 110 105 -4%
1000 numbers 42.2 25.0 -41%
2000 custom values 653 393 -40%
2000 refs, 200 custom values 363 267 -26%
Uint8Array, 100 B 0.50 0.48 -5%
Uint8Array, 10 KB 6.32 6.28 -1%
Uint8Array, 1 MB 587 587 -0%

stringify

Benchmark main (µs per call) This PR (µs per call) Change
small object 0.55 0.33 -40%
100 records 177 70.1 -60%
1000 records 1,959 750 -62%
1000 records, shared authors 1,669 681 -59%
unique short strings 92.7 23.5 -75%
unique long strings 282 181 -36%
strings with an emoji 118 27.1 -77%
emoji-dense strings 173 137 -21%
escaped strings 235 92.8 -61%
string Map 188 66.4 -65%
1000 numbers 127 54.1 -57%
repeated long strings 34.0 36.6 +8%
2000 custom values 1,182 673 -43%
2000 refs, 200 custom values 833 397 -52%
sparse array 2.14 1.26 -41%
Uint8Array, 100 B 0.33 0.52 +61%
Uint8Array, 10 KB 1.62 1.49 -8%
Uint8Array, 1 MB 187 52.2 -72%

Payload size

The new format is also smaller, and compresses much better. Compressed sizes use gzip at level 6 (Node's default, and so Express's compression middleware's) and brotli at quality 4, typical settings for responses compressed as they are sent:

Value main raw This PR raw main gzip This PR gzip Change main brotli This PR brotli Change
small object 68 56 88 76 −14% 68 59 −13%
1000 records 302,201 274,225 40,637 18,362 −55% 25,087 12,382 −51%
1000 records, shared authors 280,520 254,333 38,552 19,362 −50% 23,406 12,501 −47%
string Map (1000 entries) 30,682 21,791 9,471 4,650 −51% 5,292 1,202 −77%
1000 numbers 20,323 16,428 4,341 2,243 −48% 2,970 1,288 −57%
2000 custom values 117,238 80,674 33,217 13,503 −59% 19,473 5,566 −71%
2000 refs, 200 custom values 66,051 51,556 18,703 7,164 −62% 11,236 3,127 −72%
Uint8Array, 10 KB 13,693 13,677 957 944 −1% 847 836 −1%

What isn't better

  • stringify of small typed arrays is slower: 61% for 100 bytes, where the fixed cost of each call and each value outweighs the bytes. From around 10 KB they're faster than on main: 8% at 10 KB and 72% at 1 MB. Base64 text skips JSON.stringify entirely: each buffer is written as a placeholder, "$B-0", and its text is spliced in afterwards.
  • stringify of repeated long strings is 8% slower.
  • uneval is unchanged, within −2% and +5%.

To reproduce: pnpm bench, or pnpm bench:compare main (that one checks out each branch, so it runs main's own benchmark files there).

Tests

  • New format.test.js (the spec's cases), fuzz.test.js (round trips and damaged payloads) and references-option.test.js.
  • The existing suites are ported to the new format.

@pkg-svelte-dev

pkg-svelte-dev Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Install the latest version of devalue from 4449f1c:

pnpm add https://pkg.svelte.dev/devalue/c/4449f1cc45bdbef1aa73635bb2a170915b1f0dbc

Open in pkg.svelte.dev: https://pkg.svelte.dev/repos/devalue/pr/246

@changeset-bot

changeset-bot Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 4449f1c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
devalue Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel-internal-security-reviewer

Copy link
Copy Markdown

Security review details

Plain JSON with $-marked values, $G-wrapped roots when there are references,
["$:Name", payload] for tagged types, and $A for promises in stringifyAsync.
Also adds the references: false option, skips symbol keys, and makes
stringifyAsync fail fast.
A box (`Object()` of a promise is the promise itself) or a reviver could
make two promises resolve with each other, so neither ever settled. Every
result is now checked once it is built, which replaces the two narrower
checks on bare `$P` results and on a promise resolving to itself.
…them in a Set

A reference to a custom value whose function reviver hasn't returned, or to
a box or view whose contents are still being read, now always throws
`Invalid circular reference`.
`path_to_root` walks the root the promise was found in again, up to the
promise, so its path is collected the way an error's is. That replaces
`locate`, which repeated the whole walk. Also inlines two one-use helpers
in parse.js, reuses `at` in `plain_object` and finds Temporal types with
`indexOf`. About 600 B smaller minified.
The writer keeps one array of root records, always created, instead of four
parallel arrays that were `null` in `stringify` behind an `any` cast. The
reader creates its map of promises when it meets the first one.
…nly() writes value

`only` no longer takes the value `writer` already has.
'devalue': major
---

breaking: a value written by `stringifyAsync` is parsed with a promise wherever it had one, where earlier versions put what the promise resolved to

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
breaking: a value written by `stringifyAsync` is parsed with a promise wherever it had one, where earlier versions put what the promise resolved to
breaking: `stringifyAsync` no longer elides promises

Comment thread .changeset/symbol-keys.md
'devalue': major
---

breaking: `stringify` and `stringifyAsync` skip an object's symbol keys, as `JSON.stringify` does, instead of throwing `Cannot stringify POJOs with symbolic keys`. `uneval` still throws

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should just skip them in uneval too. Probably unlocks extra perf there as well

Comment thread src/operations.js

createNullPrototypeObject: () => Object.create(null),

createPromise: () => {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably return { promise, resolve, reject } so that in the future, when we need rejections, we don't have to make a breaking change.

Comment thread src/parse.js
* @param {import('./types.js').StringValueTag} tag
* @param {string} value
*/
function text(tag, value) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't this just const text = (tag, value) => shadow(tag, () => ops.fromStringValue(tag, value));

A parse suite that mirrors the stringify one, parsing what the same
version's stringify writes, plus benchmarks for records that share their
authors, custom values, references to custom values and repeated long
strings. The shared values live in benchmarking/shapes.js.
… scanned

JSON.stringify scans every string for characters to escape, which for the
base64 text of an ArrayBuffer costs more than encoding it and never finds
any. Each buffer is written as a placeholder, "$B-0", and swapped in once
the root is text. Only a whole string value that is exactly "$B-<digits>"
counts as one, which nothing in the value can produce. `digits` moves to
utils.js so the reader and writer share it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant