Streaming JSON in just 200 lines of JavaScript
Briefly

Streaming JSON in just 200 lines of JavaScript
"I was continueing my exploration of React server components when I stumbled upon on this article about progressive JSON. Dan Abramov describes a technique for streaming JSON from a server to a client in chunks, allowing the client to start rendering parts of the data before the entire payload has been received. This can significantly improve perceived performance, especially for large datasets."
"The idea behind the progressive JSON streaming is to send parts of the JSON data as soon as they are available, rather than waiting for the entire JSON structure to be ready. This is particularly useful for large datasets or when the data is being generated on-the-fly. For the things that are not there yet, we can send placeholders that the client can later replace with the actual data when it arrives."
"Let's say that we have the information about the user right away, but the posts are being fetched from a database and will take some time. Instead of waiting for the posts to be ready, we can send a placeholder for the posts field: { "user": { "id": 1, "name": "John Doe", "posts": "_$1" } } Then when the posts are ready, we can send them as a separate chunk:"
Progressive JSON streaming delivers JSON in incremental chunks so parts of a payload arrive and render before the entire structure is complete. Placeholders mark fields that are delayed, and later chunks map placeholder identifiers to actual objects or arrays. Clients parse the initial payload, render available fields, and replace placeholders when matching chunks arrive, reducing perceived latency for large or on-the-fly datasets. The pattern uses placeholder keys (for example "_$1") in the main JSON and separate chunks that provide the real data. A compact implementation named Streamson demonstrates this approach in roughly 200 lines of code.
Read at krasimirtsonev.com
Unable to calculate read time
[
|
]