Repository: facebook/react
File: packages/react-client/src/ReactFlightClient.js (JavaScript)

Code before:
  // as `this`. This ensures parentObject is never null when accessed downstream.
  return response._walkJSON(parsed, {'': parsed}, '');
}

function createWalkParsedJSON(response: Response) {
  function walk(
    value: JSONValue,
    parentObject: Object | null,
    key: string,
  ): any {
    if (typeof value === 'string') {
      if (value[0] === '$') {
        return parseModelString(response, parentObject, key, value);
      }
      return value;
    }
    if (typeof value !== 'object' || value === null) {
      return value;
    }
    if (Array.isArray(value)) {
      for (let i = 0; i < value.length; i++) {
        (value: any)[i] = walk(value[i], value, '' + i);
      }
      if (value[0] === REACT_ELEMENT_TYPE) {
        // React element tuple
        return parseModelTuple(response, value);
      }
      return value;
    }
    // Plain object
    for (const k in value) {
      if (k === __PROTO__) {
        delete (value: any)[k];
      } else {
        const walked = walk((value: any)[k], value, k);
        if (walked !== undefined) {
          (value: any)[k] = walked;
        } else {
          delete (value: any)[k];
        }
      }
    }
    return value;
  }
  return walk;
}

export function close(weakResponse: WeakResponse): void {
  // In case there are any remaining unresolved chunks, they won't be resolved
  // now. So we either error or halt them depending on whether partial streams
  // are allowed.

Review comment (performance):
Curious about the performance comparison here vs. the initial version's element shortcut. The shortcut avoided the generic loop for element arrays entirely, processing type/key/props with type-specific knowledge (e.g. skipping `_walkJSON` for a plain string type or null key), avoiding `'' + i` string conversion per index, and not re-reading fields from the array through `parseModelTuple`. With the post-loop approach, element arrays go through the full generic walk, then `parseModelTuple` re-accesses all fields that were just walked. Was there a measured reason to drop the shortcut, or was it just for simplicity?

Code after:
  response: Response,
  value: JSONValue,
  parentObject: Object,
  key: string,
): any {
  if (typeof value === 'string') {
    if (value[0] === '$') {
      return parseModelString(response, parentObject, key, value);
    }
    return value;
  }
  if (typeof value !== 'object' || value === null) {
    return value;
  }
  if (isArray(value)) {
    for (let i = 0; i < value.length; i++) {
      (value: any)[i] = reviveModel(response, value[i], value, '' + i);
    }
    if (value[0] === REACT_ELEMENT_TYPE) {
      // React element tuple
      return parseModelTuple(response, value);
    }
    return value;
  }
  // Plain object
  for (const k in value) {
    if (k === __PROTO__) {
      delete (value: any)[k];
    } else {
      const walked = reviveModel(response, (value: any)[k], value, k);
      if (walked !== undefined) {
        (value: any)[k] = walked;
      } else {
        delete (value: any)[k];
      }
    }
  }
  return value;
}

export function close(weakResponse: WeakResponse): void {
  // In case there are any remaining unresolved chunks, they won't be resolved
  // now. So we either error or halt them depending on whether partial streams
  // are allowed.
  // TODO: Ideally we should be able to bail out early if we kept a ref count of
  // pending chunks.
  if (hasGCedResponse(weakResponse)) {
    return;
  }
  const response = unwrapWeakResponse(weakResponse);
  if (response._allowPartialStream) {