Skip to content
Advertisement

Invalid array length allocation failed

I have the following script:

    for (var i = 0; i < obj.length; i++) {
        obj.push("It's nice to work at Bass Pro Shop!"); 
    }

    return obj;
}

function validate(reason) {
    return reason.split(' ').length < 3
}

Output

I’m receiving this error when trying to run my JS:

FATAL ERROR: invalid array length Allocation failed – JavaScript heap out of memory

I’ve checked other similar posts that say to increase the memory size f.x., node --max-old-space-size=16000 yourFile.js or from FATAL ERROR to my script they both don’t work. Any help would be greatly appreciated.

Advertisement

Answer

Took me a while to write your code down. You shouldn’t be posting images of code.

Anyway, the problem lies here:

for(var i = 0; i < obj.length; i++)
{
    obj.push("It's nice to work at Devoteam!");
}

You are looping the same array to where you push more data inside the loop, thus the loop will never end and the array size becomes too large.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement