Js: view the execution time of a function. Example code _ javascript tips-js tutorial

Source: Internet
Author: User
This article will share with you an example of code. It mainly describes how to view the execution time of a function in js. The code is easy to understand. If you are interested, follow the editor to learn. Js: view the execution time of a function. Example code _ javascript skills

For more information, see the following code examples.

The following is a summation function. We need to know the time required for executing this function.


Function add () {var sum = 0; for (var I = 0; I <1000000; I ++) {sum + = I;} return sum ;}


Define a test function and pass the function to be tested as a parameter

Function test (func) {var start = new Date (). getTime (); // start time func (); // execute the var end = new Date () function to be tested (). getTime (); // receipt time return (end-start) + "ms"; // time required for returning function execution}

Test and view the actual execution time

Var time = test (add); console. log (time );

The above is the javascript code for viewing the execution time of a function instance _ javascript skills. For more information, see the PHP Chinese website (www.php1.cn )!

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.