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 )!