Last active
November 2, 2023 20:56
-
-
Save OMartinez-NeT/74ef26bdebb43359b74d8c0b7ed747c8 to your computer and use it in GitHub Desktop.
Stringify large array of objects (over 500 mb) avoid RangeError: Invalid string length
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function largeStringify<T>(resources: T[]): Promise<Buffer> { | |
const blobs = [new Blob(["["])]; | |
resources.forEach((resource, index) => { | |
const resourceString = JSON.stringify(resource); | |
blobs.push(new Blob([resourceString])); | |
if (index !== resources.length - 1) { | |
blobs.push(new Blob([","])); | |
} | |
}); | |
blobs.push(new Blob(["]"])); | |
const finalBlob = new Blob(blobs); | |
return finalBlob.arrayBuffer().then(arrayBuffer => { | |
return Buffer.from(arrayBuffer); | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment