Fixing "Error: script stack space quota is exhausted" in Firefox 3.x

Posted by james on Feb. 1, 2011

I've gotten the "Error: script stack space quota is exhausted" problem before. It usually happens when I'm doing an unreasonable amount of data manipulation in Javascript, but it's still a problem.

I recently found a fix for a specific circumstance. I narrowed down the culprit to this line:

var elem = $('' + html + '');

The problem here is that the "html" var is a 500k string containing html markup. jQuery seems to be inefficient for this purpose, as running it through the $('') should create a jQuery node containing a new HTML DOM element with the contents of "html".

Using this instead fixed problem:

var elem = $('');
elem.innerHTML = html;

I suspected that innerHTML would be more efficient than jQuery's HTML parsing, and I was right. So in this specific circumstance, I found a way to avoid the issue. The bigger problem is still there: why the heck am I parsing 500k worth of HTML text through Javascript? No good reason, unfortunately.