@samitouri / QOS-React-1 / commits / f247ebaf44

[Flight] Walk parsed JSON instead of using reviver for parsing RSC payload (#35776)

## Summary Follow-up to https://github.com/vercel/next.js/pull/89823 with the actual changes to React. Replaces the `JSON.parse` reviver callback in `initializeModelChunk` with a two-step approach: plain `JSON.parse()` followed by a recursive `reviveModel()` post-process (same as in Flight Reply Server). This yields a **~75% speedup** in RSC chunk deserialization. | Payload | Original (ms) | Walk (ms) | Speedup | |---------|---------------|-----------|---------| | Small (2 elements, 142B) | 0.0024 | 0.0007 | **+72%** | | Medium (~12 elements, 914B) | 0.0116 | 0.0031 | **+73%** | | Large (~90 elements, 16.7KB) | 0.1836 | 0.0451 | **+75%** | | XL (~200 elements, 25.7KB) | 0.3742 | 0.0913 | **+76%** | | Table (1000 rows, 110KB) | 3.0862 | 0.6887 | **+78%** | ## Problem `createFromJSONCallback` returns a reviver function passed as the second argument to `JSON.parse()`. This reviver is called for **every key-value pair** in the parsed JSON. While the logic inside the reviver is lightweight, the dominant cost is the **C++ → JavaScript boundary crossing** — V8's `JSON.parse` is implemented in C++, and calling back into JavaScript for every node incurs significant overhead. Even a trivial no-op reviver `(k, v) => v` makes `JSON.parse` **~4x slower** than bare `JSON.parse` without a reviver: ``` 108 KB payload: Bare JSON.parse: 0.60 ms Trivial reviver: 2.95 ms (+391%) ``` ## Change Replace the reviver with a two-step process: 1. `JSON.parse(resolvedModel)` — parse the entire payload in C++ with no callbacks 2. `reviveModel` — recursively walk the resulting object in pure JavaScript to apply RSC transformations The `reviveModel` function includes additional optimizations over the original reviver: - **Short-circuits plain strings**: only calls `parseModelString` when the string starts with `$`, skipping the vast majority of strings (class names, text content, etc.) - **Stays entirely in JavaScript** — no C++ boundary crossings during the walk ## Results You can find the related applications in the [Next.js PR ](https://github.com/vercel/next.js/pull/89823)as I've been testing this on Next.js applications. ### Table as Server Component with 1000 items Before: ```     "min": 13.782875000000786,     "max": 22.23400000000038,     "avg": 17.116868530000083,     "p50": 17.10766700000022,     "p75": 18.50787499999933,     "p95": 20.426249999998618,     "p99": 21.814125000000786 ``` After: ```     "min": 10.963916999999128,     "max": 18.096083000000363,     "avg": 13.543286884999988,     "p50": 13.58350000000064,     "p75": 14.871791999999914,     "p95": 16.08429099999921,     "p99": 17.591458000000785 ``` ### Table as Client Component with 1000 items Before: ```     "min": 3.888875000000553,     "max": 9.044959000000745,     "avg": 4.651271475000067,     "p50": 4.555749999999534,     "p75": 4.966624999999112,     "p95": 5.47754200000054,     "p99": 6.109499999998661 ```` After: ```     "min": 3.5986250000005384,     "max": 5.374291000000085,     "avg": 4.142990245000046,     "p50": 4.10570799999914,     "p75": 4.392041999999492,     "p95": 4.740084000000934,     "p99": 5.1652500000000146 ``` ### Nested Suspense Before: ``` Requests: 200 Min: 73ms Max: 106ms Avg: 78ms P50: 77ms P75: 80ms P95: 85ms P99: 94ms ``` After: ``` Requests: 200 Min: 56ms Max: 67ms Avg: 59ms P50: 58ms P75: 60ms P95: 65ms P99: 66ms ``` ### Even more nested Suspense (double-level Suspense) Before: ``` Requests: 200 Min: 159ms Max: 208ms Avg: 169ms P50: 167ms P75: 173ms P95: 183ms P99: 188ms ``` After: ``` Requests: 200 Min: 125ms Max: 170ms Avg: 134ms P50: 132ms P75: 138ms P95: 148ms P99: 160ms ``` ## How did you test this change? Ran it across many Next.js benchmark applications. The entire Next.js test suite passes with this change. --------- Co-authored-by: Hendrik Liebau <mail@hendrik-liebau.de>

Tim Neutkens committed Feb 19, 2026 at 17:37 UTC f247ebaf44317ac6648b62f99ceaed1e4fc4dc01
2 files changed +49 -19
packages/react-client/src/ReactFlightClient.js
+49 -16
@@ -355,7 +355,6 @@ type Response = {
355 _encodeFormAction: void | EncodeFormActionCallback,
356 _nonce: ?string,
357 _chunks: Map<number, SomeChunk<any>>,
358 - _fromJSON: (key: string, value: JSONValue) => any,
358 _stringDecoder: StringDecoder,
359 _closed: boolean,
360 _closedReason: mixed,
@@ -2302,6 +2301,11 @@ function defineLazyGetter<T>(
2301 // TODO: We should ideally throw here to indicate a difference.
2302 return OMITTED_PROP_ERROR;
2303 },
2304 + // no-op: the walk function may try to reassign this property after
2305 + // parseModelString returns. With the JSON.parse reviver, the engine's
2306 + // internal CreateDataProperty silently failed. We use a no-op setter
2307 + // to match that behavior in strict mode.
2308 + set: function () {},
2309 enumerable: true,
2310 configurable: false,
2311 });
@@ -2622,6 +2626,11 @@ function parseModelString(
2626 // TODO: We should ideally throw here to indicate a difference.
2627 return OMITTED_PROP_ERROR;
2628 },
2629 + // no-op: the walk function may try to reassign this property
2630 + // after parseModelString returns. With the JSON.parse reviver,
2631 + // the engine's internal CreateDataProperty silently failed.
2632 + // We use a no-op setter to match that behavior in strict mode.
2633 + set: function () {},
2634 enumerable: true,
2635 configurable: false,
2636 });
@@ -2699,7 +2708,6 @@ function ResponseInstance(
2708 this._nonce = nonce;
2709 this._chunks = chunks;
2710 this._stringDecoder = createStringDecoder();
2702 - this._fromJSON = (null: any);
2711 this._closed = false;
2712 this._closedReason = null;
2713 this._allowPartialStream = allowPartialStream;
@@ -2783,9 +2791,6 @@ function ResponseInstance(
2791 markAllTracksInOrder();
2792 }
2793 }
2786 -
2787 - // Don't inline this call because it causes closure to outline the call above.
2788 - this._fromJSON = createFromJSONCallback(this);
2794 }
2795
2796 export function createResponse(
@@ -5259,24 +5264,52 @@ export function processStringChunk(
5264 }
5265
5266 function parseModel<T>(response: Response, json: UninitializedModel): T {
5262 - return JSON.parse(json, response._fromJSON);
5267 + const rawModel = JSON.parse(json);
5268 + // Pass a wrapper object as parentObject to match the original JSON.parse
5269 + // reviver behavior, where the root value's reviver receives {"": rootValue}
5270 + // as `this`. This ensures parentObject is never null when accessed downstream.
5271 + return reviveModel(response, rawModel, {'': rawModel}, '');
5272 }
5273
5265 -function createFromJSONCallback(response: Response) {
5266 - // $FlowFixMe[missing-this-annot]
5267 - return function (key: string, value: JSONValue) {
5268 - if (key === __PROTO__) {
5269 - return undefined;
5274 +function reviveModel(
5275 + response: Response,
5276 + value: JSONValue,
5277 + parentObject: Object,
5278 + key: string,
5279 +): any {
5280 + if (typeof value === 'string') {
5281 + if (value[0] === '$') {
5282 + return parseModelString(response, parentObject, key, value);
5283 }
5271 - if (typeof value === 'string') {
5272 - // We can't use .bind here because we need the "this" value.
5273 - return parseModelString(response, this, key, value);
5284 + return value;
5285 + }
5286 + if (typeof value !== 'object' || value === null) {
5287 + return value;
5288 + }
5289 + if (isArray(value)) {
5290 + for (let i = 0; i < value.length; i++) {
5291 + (value: any)[i] = reviveModel(response, value[i], value, '' + i);
5292 }
5275 - if (typeof value === 'object' && value !== null) {
5293 + if (value[0] === REACT_ELEMENT_TYPE) {
5294 + // React element tuple
5295 return parseModelTuple(response, value);
5296 }
5297 return value;
5279 - };
5298 + }
5299 + // Plain object
5300 + for (const k in value) {
5301 + if (k === __PROTO__) {
5302 + delete (value: any)[k];
5303 + } else {
5304 + const walked = reviveModel(response, (value: any)[k], value, k);
5305 + if (walked !== undefined) {
5306 + (value: any)[k] = walked;
5307 + } else {
5308 + delete (value: any)[k];
5309 + }
5310 + }
5311 + }
5312 + return value;
5313 }
5314
5315 export function close(weakResponse: WeakResponse): void {
packages/react-noop-renderer/src/ReactNoopFlightClient.js
-3
@@ -43,9 +43,6 @@ const {createResponse, createStreamState, processBinaryChunk, getRoot, close} =
43 requireModule(idx: string) {
44 return readModule(idx);
45 },
46 - parseModel(response: Response, json) {
47 - return JSON.parse(json, response._fromJSON);
48 - },
46 bindToConsole(methodName, args, badgeName) {
47 return Function.prototype.bind.apply(
48 // eslint-disable-next-line react-internal/no-production-logging