As mentioned by @sandeepanu, there's a great little solution by @madhunimmo for if you're trying to stringify a huge array. Just stringify one element at a time:
let out = "[" + yourArray.map(el => JSON.stringify(el)).join(",") + "]";
If you're trying to stringify an object with a very large number of keys/properties, then you could just use Object.entries()
on it first to turn it into an array of key/value pairs first:
let out = "[" + Object.entries(yourObject).map(el => JSON.stringify(el)).join(",") + "]";
If that still doesn't work, then you'll probably want to use a streaming approach, although you could slice your array into portions and store as multiple jsonl
(one object per line) files:
// untested code
let numFiles = 4;
for(let i = 0; i < numFiles; i++) {
let out = arr.slice((i/numFiles)*arr.length, ((i+1)/numFiles)*arr.length).map(el => JSON.stringify(el)).join(",");
// add your code to store/save `out` here
}
One streaming approach (new, and currently only supported in Chrome, but will likely come to other browsers, and even Deno and Node.js in some form or another) is to use the File System Access API. The code would look something like this:
// untested code
const dirHandle = await window.showDirectoryPicker();
const fileHandle = await dirHandle.getFileHandle('yourData.jsonl', { create: true });
const writable = await fileHandle.createWritable();
for(let el of yourArray) {
await writable.write(JSON.stringify(el)+"\n");
}
await writable.close();