Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increasing stack size in browsers

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!

like image 285
iwicopd2 Avatar asked Feb 13 '16 14:02

iwicopd2


People also ask

What is maximum stack size exceeded?

The JavaScript RangeError: Maximum call stack size exceeded is an error that occurs when there are too many function calls, or if a function is missing a base case.

How can we avoid maximum call stack size exceeded?

Non-Terminating Recursive Functions When you call a recursive function, again and again, this limit is exceeded and the error is displayed. So, call recursive functions carefully so that they terminate after a certain condition is met. This will prevent the maximum call stack to overflow and the error will not pop up.

What is the limit of a call stack?

Without any local variables, each function call takes up 48 bytes during the execution, and you are limited to less than 1MB for all local function frames. Each boolean and number variable takes 8 bytes of memory.

How big is the stack in JavaScript?

How big is the stack size? Stacks are temporary memory address spaces used to hold arguments and automatic variables during invocation of a subprogram or function reference. In general, the default main stack size is 8 megabytes.


1 Answers

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.

like image 51
Clément Avatar answered Sep 21 '22 02:09

Clément