title: Streaming / Piping JSON.stringify output in Node.js / Express
body: <p>I have a scenario where I need to return a very large object, converted to a JSON string, from my Node.js/Express RESTful API.</p>
  
  <pre><code>res.end(JSON.stringify(obj));
  </code></pre>
  
  <p>However, this does not appear to scale well. Specifically, it works great on my testing machine with 1-2 clients connecting, but I suspect that this operation may be killing the CPU &amp; memory usage when many clients are requesting large JSON objects simultaneously.</p>
  
  <p>I've poked around looking for an async JSON library, but <a href="https://github.com/ckknight/async-json" rel="noreferrer">the only one I found</a> seems to have an issue (specifically, I get a [RangeError]). Not only that, but it returns the string in one big chunk (eg, the callback is called once with the entire string, meaning memory footprint is not decreased).</p>
  
  <p>What I really want is a completely asynchronous piping/streaming version of the JSON.stringify function, such that it writes the data as it is packed directly into the stream... thus saving me both memory footprint, and also from consuming the CPU in a synchronous fashion.</p>