Console. time and console. timeEnd usage

Source: Internet
Author: User

The console. time and console. timeEnd methods allow WEB developers to measure the time consumed by executing a javascript script program. As WEB applications become more and more important, and JavaScript execution performance is also receiving increasing attention, WEB developers know that some performance testing machines are necessary. The console. time and console. timeEnd to be introduced today are one of them.

The console. time method is used to start the computing time, and the console. timeEnd method is used to stop the timing and output the script execution time.

The code is as follows: Copy code

// Start the timer
Console. time ('testforeach ');

// (Write some test code)

// Stop timing and output time
Console. timeEnd ('testforeach ');

// 4522.303 ms

Each of these two methods can pass a parameter as the timer name, which is used to identify each timer when the code runs in parallel. The call to console. timeEnd will immediately output the total execution time, in milliseconds.

There are many methods to test JavaScript performance, but the console. time/timeEnd methods are the most basic and straightforward techniques.

If you need to know the code execution time during Web Debugging, you can add the console in JavaScript code. time () statement and console. timeEnd () statement to timing program execution. The following is an example of the time-consuming foo () function:

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 before calling the foo () function. time () statement, which is inserted into the console after the call is completed. timeEnd () statement:

The code is as follows: Copy code


Console. time ("test ");
Foo ();
Console. timeEnd ("test ");

After the program is executed, the console displays the timing result: "test: 1797ms" and the log level is info.

Console. time () and console. timeEnd () accept a string as the parameter, which is equivalent to the timing id. The browser will pair 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.