Home  >  Article  >  Web Front-end  >  Detailed introduction to the console.time() function in JavaScript_javascript skills

Detailed introduction to the console.time() function in JavaScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:23:491430browse

If you need to know the time when the code is executed during web debugging, you can time the execution of the program by adding console.time() statements and console.timeEnd() statements in the JavaScript code. Take the following lengthy foo() function as an example:


Copy code 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 a console.time() statement before the foo() function call and a console.timeEnd() statement after the call ends:


Copy code The code is as follows:

console.time("test");
foo();
console.timeEnd("test");


After the program is executed, the console will display the result of this timing: "test: 1797ms", and the displayed log level is info.

console.time() and console.timeEnd() accept a string as a parameter, which is equivalent to the timing id. The browser will pair console.time() with the same parameter (id) and console.timeEnd() and record the time difference between the two. Therefore, it is possible to time different places in a JavaScript program by using different ids.

Browser support

For each browser, console.time() timing support is as follows:

Firefox. Native support after 10.0. For previous versions of Firefox, this can be achieved by installing the Firebug plug-in. For details, see: https://developer.mozilla.org/en-US/docs/Web/API/console.time?redirectlocale=en-US&redirectslug=DOM/console.time
Google Chrome. Native support after 2.0. For details, see: https://developers.google.com/chrome-developer-tools/docs/console-api#consoletimelabel
ie. Supported natively in IE11. For previous versions of IE, this can be achieved by installing Firebug Lite. For details, see: http://msdn.microsoft.com/en-us/library/ie/dn265071(v=vs.85).aspx
Safari. Native support after 4.0. For details, see: https://developer.apple.com/library/safari/documentation/AppleApplications/Conceptual/Safari_Developer_Guide/Console/Console.html
Opera. support. For details, see: http://www.opera.com/dragonfly/documentation/console/

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn