Console.time () is a function that JavaScript uses to display the runtime of the code, and now we're going to talk about this function in more detail and application examples.
If you need to know when your code executes during Web debugging, you can use the Console.time () statement and the Console.timeend () statement to time the execution of your program in JavaScript code. Take this longer-consuming Foo () function for example:
| 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.time () statement before the Foo () function call, and insert the Console.timeend () statement after the end of its call:
| The code is as follows |
|
Console.time ("test"); Foo (); Console.timeend ("test"); |
After the program is finished, the console displays the result of this timing: "Test:1797ms", which displays a log level of info.
Console.time () and Console.timeend () accept a string as the parameter, which is equivalent to the timed ID. The browser will match the 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
For each browser, the support for Console.time () timing is as follows:
Firefox. 10.0 native support. For previous versions of Firefox, this can be done by installing the Firebug plugin. See: https://developer.mozilla.org/en-US/docs/Web/API/console.time?redirectlocale=en-US&redirectslug=DOM% 2fconsole.time
Google Chrome. 2.0 native support.
See: Https://developers.google.com/chrome-developer-tools/docs/console-api#consoletimelabel
Ie. IE11 Central Plains students support. For previous versions of IE, it can be accomplished by installing Firebug Lite.
See: Http://msdn.microsoft.com/en-us/library/ie/dn265071%28v=vs.85%29.aspx
Safari. 4.0 native support.
Opera. Support.