Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What counts as an "executed script statement" for IE's "Stop running this script?" warning counter?

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:

  1. Would var a = 1, b = 2, c = 3; count as one "executed script statement" and var a = 1; var b = 2; var c = 3; count as three? Or would both be three?
  2. Would if( someFunction() ){} (not counting statements within someFunction() be one statement, or two (a call plus a conditional)?
  3. Is if(a){}else{} one conditional statement or two? If one, would if(a){}else if(b){} be two?
  4. Is if(a==b||(c&&a==c&&c==d)){} one, two, three, four, five statements (or more?)? I'm aware that anything like if(a){} calls a Javascript function to convert to boolean - would this add additional statements on top of the comparisons themselves?
  5. 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?
  6. 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.
  7. 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

like image 944
user56reinstatemonica8 Avatar asked Nov 02 '22 05:11

user56reinstatemonica8


1 Answers

What you are asking for can only be guessed unless some Microsoft IE8 developper comes and answer your question.
Being a puny Froggy with no connections with Microsoft whatsoever, I designed a simple test to try and measure how statements are counted:

<body>
<div id='div'></div>
<script>
var count = 5000000;
var res = 0;
var d = document.getElementById ('div');
function fun (aaa) // 1 statement
{
    aaa+= Math.random(); // 1 statement
    aaa+= Math.random(); // 1 statement
    aaa+= Math.random(); // 1 statement
}
function format (count) // 1 statement
{
    return Math.round(count*100/i)/100; // 1 statement
}
function format2 (count) // 1 statement
{
    var a = count*100; //4 statements
    var b = a / i;
    var c = Math.round (b);
    var d = c/100;
    return d; // 1 statement
}

for (var i = 0 ; i != count ; i++) // 2 statements
{
    res += Math.random();                      // 1 statement
    d.innerHTML = format2(count);              // 1 statement
    d.innerHTML = Math.round(count*100/i)/100; // 1 statement
    fun (res);                                 // 1 statement
}
</script>
</body>

I ran this script on IE8 over XP in a VirtualBox.

The number displayed in the page represents roughly the number of statements (as IE counts them) per loop. The imprecision is probably due to the asynchronous polling of the statements count, as explained in the Microsoft paper.

You can modify the code to see for yourself which kind of instructions are counted as statements.

My empirical observations so far :

It seems a statement is indeed close to the grammatical definition, i.e. anything found until the next ;.

  • a loop counts as 2 statements (the test and the indice update, probably)
  • a function call counts as 2 statements, regardless of the result being affected to a variable
  • an arbitrarily complex expression evaluation counts as a single statement
  • builtin functions dont count as statements, but each statement executed in user defined ones does.

It seems you could cram more power inside the 5M allowed statements by packing computations into more complex single statements.

All this being said, a design that uses a browser to perform heavy duty computations is fundamentally flawed, if you ask me.

like image 154
kuroi neko Avatar answered Nov 15 '22 04:11

kuroi neko