Home > Web Front-end > JS Tutorial > Detailed introduction to the console.time() function in JavaScript

Detailed introduction to the console.time() function in JavaScript

高洛峰
Release: 2017-02-03 14:56:28
Original
1815 people have browsed it

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 the console.time() statement and console.timeEnd() statement to the JavaScript code. Take the following foo() function, which takes a long time, as an example:

function foo(){
    var x = 4.237;
    var y = 0;
    for (var i=0; i<100000000; i++) {
        y = y + x*x;
    }
    return y;
}
Copy after login

If you need to know how long it takes to execute the function, you can insert a console.time() statement before the foo() function call. , insert the console.timeEnd() statement after the call is completed:

console.time("test");
foo();
console.timeEnd("test");
Copy after login

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.

For more detailed introduction to the console.time() function in JavaScript and related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template