Have I reached the limits of the size of objects JavaScript in my browser can handle?

Here’s what I would try: you said it’s a 44MB file. That surely takes more than 44MB of memory, I’m guessing this takes much over 44MB of RAM, maybe half a gig. Could you just cut down the data until the browser doesn’t crash and see how much memory the browser uses?

Even apps that run only on the server would be well served to not read a 44MB file and keep it in memory. Having said all that, I believe the browser should be able to handle it, so let me run some tests.

(Using Windows 7, 4GB of memory)

First Test
I cut the array in half and there were no problems, uses 80MB, no crash

Second Test
I split the array into two separate arrays, but still contains all the data, uses 160Mb, no crash

Third Test
Since Firefox said it ran out of stack, the problem is probably that it can’t parse the array at once. I created two separate arrays, arr1, arr2 then did arr3 = arr1.concat(arr2); It ran fine and uses only slightly more memory, around 165MB.

Fourth Test I am creating 7 of those arrays (22MB each) and concatting them to test browser limits. It takes about 10 seconds for the page to finish loading. Memory goes up to 1.3GB, then it goes back down to 500MB. So yeah chrome can handle it. It just can’t parse it all at once because it uses some kind of recursion as can be noticed by the console’s error message.

Answer Create separate arrays (less than 20MB each) and then concat them. Each array should be on its own var statement, instead of doing multiple declarations with a single var.

I would still consider fetching only the necessary part, it may make the browser sluggish. however, if it’s an internal task, this should be fine.

Last point: You’re not at maximum memory levels, just max parsing levels.

Leave a Comment