setTimeout() causing Allocation Failure w/ JS stack trace

So here is my code: https://repl.it/@Michael_Nicol/JavaScript-Playground

It may look like a lot, but the part I am struggling on is line 38

Now if you read through the comments, you can see on line 19 I make a function that generates a random combination of the string input (now an array).

Note this is just a snippet of a bigger function, but I included all the variables so it will still work. The output should be a list of every combination of 123 (no repeats).

123, 132, 213, 231, 312, 321

On 35, you can see I have a loop to call the randomCombo() function with the setTimeout() function with a delay of 1 millisecond(s). This makes a random combination of my string (really an array).

On line 42 I go through the combinations and if this new combo matches a combination in my list of combinations, I then give it a length of 0 (empty array).

On line 50 I check the new combination and if I set it to an empty array in the previous loop I then ignore it and do now add it. otherwise, I do add it to my list of combinations.

On line 57 I reset my variable and then do this all over again until I get my desired combinations per my fact (factorial) variable.

Now when you click to run it, it takes forever to run and it should only take < 60 milliseconds. If it is anything over a second, something is off. Then i get this thing:

Here is a short snippet of it:

<— JS stacktrace —>

==== JS stack trace =========================================

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memoryHopefully, this made sense.

What does this mean? Why am I getting this?

You are calling the function, you should just pass it as the callback to setTimeout.

You have:
setTimeout(randomCombo(), 10)
Should be:
setTimeout(randomCombo, 10)

I still get an error when I run the code though. Edit: it is the comment at the bottom.

1 Like

Thanks! I fixed that and updated my issue. Now it isn’t loading at all despite having a 1 millisecond delay (changed from 10). When it doses load, its a mass of text that i have no idea what any of it means.

I edited my original topic post to include this issue.

I’d assume you have an infinite loop and your running out of memory, I think you are stuck in the while loop.

This doesn’t look like it will never be true
if (combination.length > 0)

This will
if (combination.length >= 0)

That makes the program end but now your just pushing empty arrays onto combinations.

1 Like

This is the program before i made the setTimeout() changes: https://repl.it/@Michael_Nicol/Combination-Program-20

That line is on line 89 here and it works.

For some reason, the setTimeout() is producing empty arrays or not updating my combination variable. This is causing a permanent empty array which is why it is a infinite loop.

Why am i getting empty arrays when i call the setTimeout() function?

I don’t understand your use of setTimeout.

You have this comment

// Calls that function and waits 1 milliseconds

That is not how setTimeout works, it is the other way. It waits before executing the callback.

I also don’t get the point of it. The code inside the while loop depends on the result of running the randomCombo function, so you should be calling it normally. Otherwise, the while loop code will run before randomCombo. You are making synchronous code asynchronous, as far as I can tell, for no reason.

You can see the calls to randomCombo when using setTimeout in this repl and here is it with the normal call to randomCombo.

1 Like

The reason I am doing this was to try add a pause in between each combination attempt. This is because, with a larger number of combinations, the page will tend to crash from trying to execute too fast. I wanted to try a 1 - 10-millisecond pause to my while loop in order to prevent this.

Would this be better this setInterval()?

I doubt it is crashing from running too fast, it is likely a memory issue or you are super pegging the CPU for long enough.

Yes, but it’s not going to be that simple. Making a function async that another piece of code depends on the result of, will require you to think more carefully about the execution flow.

I would actually suggest you start by trying to make your algorithm more efficient. Have a look at how other people are solving the problem.

Then if it’s still an issue, you can look at, data chunking, using async, web workers, etc.

1 Like