I have written a small program that produces arrays, which runs quite long (almost forever ;-)):
var results = [];
var i = 1;
while (true) {
console.log(i++);
results.push([]);
}
When, instead of an empty array, I create a sparse array of length i
, the program crashes quite fast:
var results = [];
var i = 1;
while (true) {
console.log(i);
results.push(new Array(i++));
}
Actually I get up to i
equal to 17424, then I get an error message telling me
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory
Abort trap: 6
and Node.js takes me back to the console. Since the only difference is that the second one produces "larger" empty arrays than the first ones, this implies that an empty sparse array of length n
takes up n
times the space of an empty array with length 1
.
Am I right about this (specifically to Node.js)?
One more question: If I run
var results = [];
var i = 1;
while (true) {
console.log(i);
var temp = [];
temp[i++] = i;
results.push(temp);
}
then I get up to 1286175, then it again crashes with:
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory
Abort trap: 6
Why does this behave differently than the other two options?
PS: I am using Node.js 0.12.0 to run this on OS X.