The patience and _javascript skills that JavaScript performs in various browsers

Source: Internet
Author: User
Tags stop script blank page
IE: Prompts for execution of more than 500W JScript engine statements.
Firefox: Prompts for more than 10 seconds to execute.
Safari: Prompts for more than 5 seconds to execute.
Opera: No matter how long the implementation will not appear prompt, the most patient.
Chrome: Prompt for execution of more than 8 seconds (estimated value).
Note: When an alert-like modal dialog box pops up, it is not timed.

In web development, a common situation is that the browser prompts the script to run too long, stop or continue, no matter what you choose, I believe you will do everything possible to keep this dialog box away from your users. But do you know how these different browsers actually judge which scripts are in "out of control" state? The author of this article analyzes the reasons for this situation from Internet Explorer, Firefox, Safari, Chrome and opera five browsers.

"Original title" What determines that a-script is long-running?
"Original Author" Nicholas C. Zakas

The following is the translation of the original text:

The problem that Web developers often encounter and have to deal with in a timely manner is "prompt script to run too long" (or "Runaway Scripting Hints"), and these annoying dialog boxes appear when your script executes too long. The basic rule for Web developers is to not allow users to see these dialogs anytime, because it gives the impression that the code is structurally unstructured and, more simply, that your code is too burdensome.

In the words of Brendan Eich (the inventor of JavaScript), if JavaScript runs in seconds, it must be somewhere wrong. The upper limit that I can personally tolerate is probably smaller, no matter what script, executed at any time, any browser, should not exceed 100 milliseconds. If the actual execution time is longer than this limit, be sure to break the process down into smaller pieces of code.

In addition, very few people really realize what causes the script to run in different browsers for too long, even I did not delve into it. So I decided to sit down and have a good look at how we would see that annoying dialog box. To determine whether the script is out of control, there are two ways. One is based on how many statements are executed, and the time it takes to determine the execution of a script. There are slightly different ways in which browsers can determine how the script is out of control.

Internet Explorer

Internet Explorer Determines whether a script is out of control, mainly by the total number of statements executed by the JScript engine. By default, the upper limit is 5 million statements, which can be modified by the registry . When the number of statements executed by your script exceeds this limit, you will see the following window.

This dialog box prompts: "There is a script on this page that causes Internet Explorer to run slowly, and your computer may become unresponsive if you continue to run." It would be a bit too much to say, if not for technical accuracy. The dialog box has two options, either stopping the script execution or allowing the script to continue running. When this dialog box is displayed, the script has been completely stopped. If you choose to continue running the script, you will recalculate the number of statements currently executing, that is, you will see the dialog again if the number reaches the upper limit again.

Firefox

Firefox determines whether a script is out of control by the time the script engine keeps executing the code. The default limit is 10 seconds, and you can modify this value by about:config the page . Notice here that when you pop up a modal dialog box like alert, it's not timed. When the browser executes the script for the maximum time, Firefox displays a dialog box similar to the following:

Firefox's dialog box prompts: "A script for this page is currently running busy, or the script has stopped responding." You can stop executing the script and open the script in the debugger, or keep the script running. More clearly described the problems encountered, and no IE is so scary. There are three things you can do on this dialog: Stop script execution, debug scripts, or keep scripts running. As with Internet Explorer, statistics on the duration of a script are reset when the script continues to run. The Debug Script button only appears when you have Firebug installed and the page activates debugging. After you perform a debug script operation, you can display the exact location of the code snippet where the execution time is too long.

Safari

Safari also judged by the time the script engine continued to execute the script, and when I studied WebKit's source code repeatedly, I found that the default timeout was 5 seconds, and once the upper limit was reached, the following dialog box prompts:

dialog box prompts: "The script on the page URL loses Safari's response and you want to continue running the script or terminating the script". Similarly, for the user, it is not a scary hint. In Safari, you can turn off detection of runaway scripts .

Chrome

Chrome is a bit tricky with tracking technology, and runaway script detection seems to be associated with TAB's incident control (crash controls). I looked carefully at the source code, but did not find specific restrictions, but the basic certainty is that the limit is based on time, estimated in 10 seconds (or 5 seconds, or 10 seconds, always with Safari or Firefox). I'm contacting a friend in the Chrome project to see if I can get a definite message. However, if there is a runaway script in the Web page, the user will see the following dialog box:

There is no doubt that Chrome's hints are more serious than those of other browsers. Click the "Wait" button, the script will continue to run until the next upper limit, you can also click "Kill pages", directly close the page in memory of all the information, and replaced by a blank page.

Opera

Opera's situation is more interesting: he doesn't seem to have the appropriate restrictions on runaway scripting. I ran a few long tests, and even spent a few minutes, and in the process, the browser has been able to respond normally, which is very unexpected. I'm not sure if it's good or bad for the present, but at least it's going to work, isn't it?

Some suggestions

No matter what browsers your users use, you should never see a similar hint at any time. Before your Web site or Web application is released as a product, it is often necessary to do some routine performance testing. There are a number of tools available in this area, such as firebug ' s profiler (Firefox only), yui Profiler (all browsers are supported) or internet Explorer 8 ' s Profiler . You should not hesitate to find a script that executes more than 100 milliseconds, even if the scripts are not smooth on some browsers, and these scripts contain snippets of code that need to be performed over a long period of time, and the code should be reassessed through the performance testing tool. Make sure you're not using chrome as a test line, because Chrome is an order of magnitude higher on JavaScript than any other browser (and Firefox 3.1 has the latest WebKit nightly). It's best to use Internet Explorer as the baseline for testing, and then test other browsers, because whenever IE's JavaScript engine is the slowest, when you fix a problem on IE, it will work on other browsers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.