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/