Internet explorer 8 (which I need to support, non-negotiable unfortunately) displays the following warning once 5,000,000 "executed script statements" have occurred in one synchronous script execution (e.g. from a timeout or from an event handler).
Stop running this script? A script on this page is causing your web browser to run slowly. If it continues to run, your computer might become unresponsive.
I'm trying to optimise some complex, heavy-duty code to avoid this error message. I've already followed the standard advise, which is to where possible break the code into seperate asynchronous chunks using things like setTimeout()
. I can't do any more of this without creating race conditions, so I'm looking to streamline all code that happens many times e.g. within large for
loops.
To do this, I want to understand what exactly IE counts as an "executed script statement", so I will know which optimisations within my loops will make the most difference and which won't.
I've looked for standard definitions of "executed script statements", without success - most times "script statement" is used, it appears to refer to the contents of a html <script>
tag which is clearly a different sense of the term. The definition Statement (computer science) is helpful but leaves some ambiguity about how they are counted (see examples below).
Here's what Microsoft have to say on the matter (I've added paragraph breaks for readability):
As of Internet Explorer 4.0 and later versions, the time-out is no longer a fixed value based on Windows messages. Internet Explorer now tracks the total number of executed script statements and resets the value each time that a new script execution is started, such as from a timeout or from an event handler, for the current page with the script engine.
Internet Explorer displays a "long-running script" dialog box when that value is over a threshold amount.
Internet Explorer doesn’t check on each instruction to see if it is over the limit. Periodically the script engine polls Internet Explorer with the number of statements executed and Internet Explorer checks if that is over the limit. Because of this mechanism, it is possible to execute more than the default limit without the dialog if the entire script execution finishes before the script engine polls Internet Explorer.
To give some simple examples that seem ambiguous to me:
- Would
var a = 1, b = 2, c = 3;
count as one "executed script statement" andvar a = 1; var b = 2; var c = 3;
count as three? Or would both be three? - Would
if( someFunction() ){}
(not counting statements withinsomeFunction()
be one statement, or two (a call plus a conditional)? - Is
if(a){}else{}
one conditional statement or two? If one, wouldif(a){}else if(b){}
be two? - Is
if(a==b||(c&&a==c&&c==d)){}
one, two, three, four, five statements (or more?)? I'm aware that anything likeif(a){}
calls a Javascript function to convert to boolean - would this add additional statements on top of the comparisons themselves? - Would
var value = someFunction(); if( value ){}
be three since it adds an assignment, or would the function call be counted as part of the assignment statement? - What about chaining? For example, if jQuery is used, then (not counting the executed script statements within each function) is the line
$(selector).show().css(obj).appendTo($el);
one "executed script statement", or four? I'd imagine it would be four "call" statements. - Presumably, var
$someEl = $(selector).show().css(obj).appendTo($el);
would increase this to five statements - four calls plus an assignment? (IE wouldn't just count it as one assignment statement and move on?)
Naturally these simple examples above are small fry - I'm trying to "know the enemy" here to be able to judge how best to optimise complex loops.
I'm looking for either a rule of thumb or some explained examples like the above