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

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

高洛峰
高洛峰Original
2017-02-03 14:56:281610browse

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;
}

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");

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!

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