Handle XMLHttpRequest response with large data
<p>Using Chrome, I setup a <code>XMLHttpRequest</code>:</p>

<pre><code>const xhr = new XMLHttpRequest();
xhr.open(method, url, true);
...
xhr.onreadystatechange = () =&gt; {
    if (xhr.readyState === XMLHttpRequest.DONE) {
      if (isStatusCodeSuccess(xhr.status)) {
        // handle response;
      } else {
        // handle error
      }
    }
};
xhr.addEventListener('progress', event =&gt; {
    console.log(event.loaded, event.target.response.length);
});
xhr.send(data);
</code></pre>

<p>At some point I request some JSON data which total size is around 295MB uncompressed / 5.8 MB compressed. Unfortunately, when the following code is called on success, the response is an empty string.</p>

<p>Using the progress event listener, I can see that the response is correctly handled chunk by chunk, up to a certain point. Here is an excerpt of the console logs it produces:</p>

<pre><code>32768 32768
4639135 4639135
7376739 7376739
11525403 11525403
...
261489180 261489180
264684030 264684030
267880247 267880247
271037819 0
274232442 0
277428774 0
...
304018210 0
309230213 0
310445469 0
</code></pre>

<p>It looks like there is a string/allocation limitation on Chrome but I don't receive any error.</p>

<p>On firefox I receive the following error: <code>InternalError: allocation size overflow</code>.</p>

<p>I tried storing the result as it comes but I can't "empty" the Xml Http Request object as attributes are readonly.</p>

<p><strong>What is the official limitation for variables in browsers?</strong> I could not find an official answer, just some experimental ones  <a href="https://stackoverflow.com/questions/34957890/javascript-string-size-limit-256-mb-for-me-is-it-the-same-for-all-browsers">Javascript string size limit: 256 MB for me - is it the same for all browsers?</a></p>

<p><strong>Is there any way to workaround this issue and handle large POST result?</strong> apart from using WebSocket.</p>

<p>I am thinking of:</p>

<ul>
<li>specific content type to stream the data, like the <code>application/octet-stream</code></li>
<li>specific parameter to handle the data by chunk, like the <a href="https://stackoverflow.com/questions/15561508/xmlhttprequest-206-partial-content">range</a> header although the content I'm fetching is not static (it changes over time)</li>
<li>known algorithm to reduce the length of the JSON response, where I can unzip chunk by chunk the data to avoid hitting the above limitation</li>
</ul>