I need to convert a large array (Uint8Array(224337596)) inside my code. Apparently the size is to big and makes the browser crash.
Is there any workaround to maybe to this in chunks?
var encrypted = convertUint8ArrayToWordArray(mergedArray)
function convertUint8ArrayToWordArray(u8Array) {
var words = [], i = 0, len = u8Array.length;
while (i < len) {
words.push(
(u8Array[i++] << 24) |
(u8Array[i++] << 16) |
(u8Array[i++] << 8) |
(u8Array[i++])
);
}
return {
sigBytes: words.length * 4,
words: words
};
}
If your browser window freezes, than you can process the array asynchronously in batches. I've put the example into snippet
function pause() {
return new Promise(r => setTimeout(r, 0))
}
async function convertUint8ArrayToWordArray(u8Array) {
var words = [], i = 0, len = u8Array.length;
while (i < len) {
words.push(
(u8Array[i++] << 24) |
(u8Array[i++] << 16) |
(u8Array[i++] << 8) |
(u8Array[i++])
);
if (i % 100000 == 0) {
await pause();
}
}
return {
sigBytes: words.length * 4,
words: words
};
}
const bigArray = new Uint8Array(224337596);
for (let idx = 0; idx < bigArray.length; ++idx) {
bigArray[idx] = Math.floor(Math.random() * 256);
}
convertUint8ArrayToWordArray(bigArray).then((res) => {
console.log(res.words[0])
});