I am trying to reduce memory usage in a javascript web application that stores a lot of information in memory in the form of a lot of small lines. When I changed the code to use Uint8Arrayinstead String, I noticed that memory usage increased.
For example, consider the following code that creates many small lines:
var a=[];
for (let i=0; i<1000000; i++)
a.push("a".repeat(10).toUpperCase());
If you put it on a blank page and allow the memory to be used for a few seconds, it will be set to 70 MiB in Google Chrome. The following code, on the other hand:
var a=[];
for (let i=0; i<1000000; i++)
a.push(new Uint8Array(10));
uses 233 MiB of memory. A blank page without code uses about 20 MiB. On the other hand, if I create a small number of large lines / arrays, the difference becomes smaller, and in the case of a single line / array with 10,000,000 characters / records, the memory usage is almost identical.
So why do typed arrays have such a large memory overhead?
source
share