The console. time () function in JavaScript details _ javascript skills

Source: Internet
Author: User
This article mainly introduces the console in JavaScript. time () function details, console. the time () function is mainly used to count the execution time of a program. if you need to know the code execution time during Web Debugging, you can add a console to the JavaScript code. time () statement and console. timeEnd () statement to timing program execution. The following is an example of the time-consuming foo () function:


The code is as follows:


Function foo (){
Var x = 4.237;
Var y = 0;
For (var I = 0; I <100000000; I ++ ){
Y = y + x * x;
}
Return y;
}


If you need to know how long it takes to execute a function, you can insert the console before calling the foo () function. time () statement, which is inserted into the console after the call is completed. timeEnd () statement:


The code is as follows:


Console. time ("test ");
Foo ();
Console. timeEnd ("test ");


After the program is executed, the console displays the timing result: "test: 1797ms" and the log level is info.

Console. time () and console. timeEnd () accept a string as the parameter, which is equivalent to the timing id. The browser will pair console. time () with the same parameter (id) with console. timeEnd () to record the time difference between the two. Therefore, you can use different IDs to timing different places in JavaScript programs.

Browser support

Support for console. time () timing in various browsers is as follows:

Firefox. Native support after 10.0. For earlier versions of Firefox, you can install the Firebug plugin. See: https://developer.mozilla.org/en-US/docs/Web/API/console.time? Redirectlocale = en-US & redirectslug = DOM % 2Fconsole. time
Google Chrome. Native support after 2.0. See: https://developers.google.com/chrome-developer-tools/docs/console-api#consoletimelabel
IE. Central Plains support for IE11. For earlier versions of IE, you can install Firebug Lite. See: http://msdn.microsoft.com/en-us/library/ie/dn265071%28v=vs.85%29.aspx
Safari. Native support after 4.0. See: https://developer.apple.com/library/safari/documentation/AppleApplications/Conceptual/Safari_Developer_Guide/Console/Console.html
Opera. Yes. See: http://www.opera.com/dragonfly/documentation/console/

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.