If you need to know the time of code execution during the web debugging process, you can time the execution of the program by adding the console.time() statement and console.timeEnd() statement in the JavaScript code. The following long-term foo() function is an example:
The code copy is as follows:
function foo(){
var x = 4.237;
var y = 0;
for (var i=0; i<10000000; i++) {
y = y + x*x;
}
return y;
}
If you need to know how long it takes during the execution of the function, you can insert the console.time() statement before the foo() function call, and insert the console.timeEnd() statement after the call is finished:
The code copy is as follows:
console.time("test");
foo();
console.timeEnd("test");
After the program is executed, the console will display the result of this time: "test: 1797ms", and the log level displayed is info.
console.time() and console.timeEnd() accept a string as an argument, which is equivalent to the timed id. The browser will pair console.time() with the same parameter (id) with console.timeEnd() to record the time difference between the two. Therefore, different places in JavaScript programs can be timed by using different ids.
Browser support
For each browser, the console.time() timing support is as follows:
Firefox. Native support after 10.0. For previous versions of Firefox, it can be achieved 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. Native support after 2.0. See: https://developers.google.com/chrome-developer-tools/docs/console-api#consoletimelabel
IE. Native support in IE11. For previous versions of IE, it can be achieved by installing 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. support. See: http://www.opera.com/dragonfly/documentation/console/