Alberto Rivera Alberto Rivera - 1 month ago 14x
Javascript Question

How can I create an array with 200 million elements on NodeJs?

To do some benchmarking, I tried to create a huge array. The code was simple:

var hugeArray = [];
for (var i = 0; i < 2*1000*1000*100; i ++) {

After I run it with 200 million elements, I get
FATAL ERROR: invalid array length Allocation failed - JavaScript heap out of memory
. With 20 million elements, this works well, so I assume I need to change some setting.

I am using MacOS with 8 GB of RAM, if that helps.


V8's heap size is limited to 1 GB..So if you want to increase it run following

node --max-old-space-size=8000 yourScript.js

Hope this helps.