Alberto Rivera Alberto Rivera - 2 months ago 35
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 ++) {
hugeArray.push(Math.ceil(Math.random()*100))
}
console.log(hugeArray.length);


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.

Answer

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.

Comments