Increasing stack size in browsers
Asked Answered
O

2

6

Short question: I have a javascript that goes very deep in recursion. How can I increase the stack size so that I can execute it (something like "ulimit -s unlimited" in Unix systems)?

Long story: I have to draw a graph and I use Cytoscape JS (http://js.cytoscape.org/) coupled with the Dagre layout extension (https://github.com/cytoscape/cytoscape.js-dagre). The drawing algorithm goes deep in the recursion and I end up getting "Uncaught RangeError: Maximum call stack size exceeded" in Chrome and "too much recursion" in Firefox. How can I set the stack size to unlimited or very large (i.e. like "ulimit -s unlimited" in Unix systems) so that I can draw the graph?

Thank you!

Open answered 13/2, 2016 at 14:15 Comment(5)
Are you sure your algorithm is correct? Did you count the number of nested calls you had at the moment you get out of stack space? Is it a reasonable figure?Potboy
Yes, the algorithm is correct. The thing is that the graph is kind of big and the drawing algorithm goes deep in recursion. However, using other drawing algorithms (that do not use recursion), I am able to draw it fairly fast... So I am supposing that if I can increase the stack size to a reasonably large size, I will be able to draw it. I searched, but could not find any answer... In the worst case, I would need to modify Dagre algorithm to change it from recursive to iterative... But I'd like to check if are there any way of setting the stack size limit of the javascript engine of browsers.Open
Adding more context, here you can find the stack size limit of several browsers: #7827492 . I just need to increase this limit.Open
But could you report on the number of nested calls you are at before getting out of memory?Potboy
Hello. I am using a third-party library, and several functions go deep in the recursion. One of them went for around 3000 levels, however it is impractical for me to change the whole library for this. I have a workaround for now, which involves changing the stack size of Google Chrome by invoking it with --js-flags="--stack_size x" command-line arguments, with a big x. Thank you very much for the help.Open
U
-1

You cannot alter the stack size in browsers, but you can use a trick called trampolining.

You can find a working code solution here:

How to understand trampoline in JavaScript?

Unconcern answered 31/5, 2016 at 14:28 Comment(3)
The question was "How can I set the stack size to unlimited or very large?" This does not answer the question.Descent
@Kym ...even though it has been selected as the accepted answer?? :O the answer implies that it is impossible, just doesn't explicitly state it.Unconcern
Thanks for the edit. Are you sure it's actually impossible? The flag that the OP mentioned in the comments does seem increase the stack size (that effect isn't achievable from javascript, but it still works from the command line).Descent
K
2

Chrome has a flag for this:

chromium-browser --js-flags="--stack-size 2048"

You will also want to run ulimit -s unlimited before running the command above, though: otherwise, your deeply recursive Javascript code will crash Chrome.

Kym answered 1/4, 2018 at 18:23 Comment(1)
The available V8 flags can be found using --js-flags="--help".Ph
U
-1

You cannot alter the stack size in browsers, but you can use a trick called trampolining.

You can find a working code solution here:

How to understand trampoline in JavaScript?

Unconcern answered 31/5, 2016 at 14:28 Comment(3)
The question was "How can I set the stack size to unlimited or very large?" This does not answer the question.Descent
@Kym ...even though it has been selected as the accepted answer?? :O the answer implies that it is impossible, just doesn't explicitly state it.Unconcern
Thanks for the edit. Are you sure it's actually impossible? The flag that the OP mentioned in the comments does seem increase the stack size (that effect isn't achievable from javascript, but it still works from the command line).Descent

© 2022 - 2024 — McMap. All rights reserved.