Hello I'm running on a problem with this function that results in a maximum call stack exceeded error. This function is not recursive, so I don't really understand why it is exceeding the call stack.
I copied this function from some blog (maybe stackoveflow), It converts a Word Array to a Byte Array to use in pako.js.
It's used to inflate a zlib compressed string.
When the string is small It doesn't exceed the call stack, but with longer strings it exceeds it.
I've tried rewriting it with setTimeout, but it becomes very slow. Do any of you have any suggestions?
Thanks.
const wordToByteArray = function (word, length) {
let ba = [];
let i;
let xFF = 0xFF;
if (length > 0)
ba.push(word >>> 24);
if (length > 1)
ba.push((word >>> 16) & xFF);
if (length > 2)
ba.push((word >>> 8) & xFF);
if (length > 3)
ba.push(word & xFF);
return ba;
};
const wordArrayToByteArray = function(wordArray, length) {
if (wordArray.hasOwnProperty("sigBytes") && wordArray.hasOwnProperty("words")) {
length = wordArray.sigBytes;
wordArray = wordArray.words;
}
let result = [];
let bytes;
let i = 0;
while (length > 0) {
bytes = wordToByteArray(wordArray[i], Math.min(4, length));
length -= bytes.length;
result.push(bytes);
i++;
}
return [].concat.apply([], result);
};
Solution Thanks for the answers bellow, this was the solution.
...
while (length > 0) {
bytes = wordToByteArray(wordArray[i], Math.min(4, length));
length -= bytes.length;
bytes.forEach(function (byte) {
result.push(byte);
});
i++;
}
return result;
};