Maximum call stack size exceeded

I’m having a problem with a function that is exceeding the maximum call stack size, due to excessive iterations. The iterations are intentional because I’m trying to create random strings that satisfy the option criteria selected by the user.

Because of the number of iterations, I sometimes get a Uncaught range error: Maximum call stack size exceeded. It usually happens when I want to create random shuffled password strings that are 128 characters long. Shorter strings don’t have a problem. I’m wondering if there’s a way to clear the stack after so many iterations or write this code some other way to avoid blowing out the stack. Note: str is based on a combination of character strings (Letter, Numeric, and Special Character) being passed to the Shuffle Code to build the passwords. And maxlength is the length of the string that I want created.

The function (s) are:

function cryptoRand() {

const randomBuffer = new Uint32Array(1);
(window.crypto || window.msCrypto).getRandomValues(randomBuffer);
return ( randomBuffer[0] / (0xffffffff + 1) );

}

function shuffleString(str, maxlength) {

var shuffledString = str.split('').sort(function(){return 0.5-cryptoRand()}).join(''");
if (maxlength > 0) {
    shuffledString = shuffledString.substr(0, maxlength);
}
return shuffledString;

}

Suggest making a lighter-weight sort function, by precomputing an array of negative and positive numbers to use (wrap around the array to cater for large number of lookups).

This topic was automatically closed after 70 days. New replies are no longer allowed.