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 |
Copy Code |
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:
code is as follows |
copy code |
console.time (" Test "); Foo (); Console.timeend ("test"); |
When the
Program finishes executing, the console displays the result of this timing: "Test:1797ms", which displays a log level of info. The
Console.time () and Console.timeend () accept a string as a 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 Console.time () timing support 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. See: Https://developer.apple.com/library/safari/documentation/AppleApplications/Conceptual/Safari_Developer_ Guide/console/console.html
Opera. Support. See: http://www.opera.com/dragonfly/documentation/console/