js View the execution time of a function example code_javascript skills
Not much to say in detail, please see the code example explanation below
There is a summation as follows function, we need to know the time it takes to execute 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 under test as a parameter
function test(func){ var start = new Date().getTime();//起始时间 func();//执行待测函数 var end = new Date().getTime();//接受时间 return (end - start)+"ms";//返回函数执行需要时间 }
Test and view the actual execution time
var time = test(add); console.log(time);
The above is the js view of the execution time of a function example code_javascript skills. For more related content, please pay attention to the PHP Chinese website (www. php.cn)!