Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I calculate how much time a function takes in jQuery?

I have jQuery functions; e.g A(), B() and C() Each function makes some Ajax calls to different sites.

I want to calculate how much time it takes to run each function (I guess in milliseconds) I just want to test my code in long loops and in different modern browsers (Safari/Chrome/IE10/Mozilla). More specifically, I want to calculate how much time it takes to take a callback from each Ajax request (this is also the time that the function ends right?) How might I achieve this?

like image 926
vorillaz Avatar asked Sep 06 '11 01:09

vorillaz


People also ask

How do you figure out how long a function takes?

The easiest way to track execution time is to use a date object. Using Date. now() that returns the total number of milliseconds elapsed since the Unix epoch, we can store the value before and after the execution of the function to be measured and then get the difference of the two.

How do you count the number of times a function is called in Javascript?

count() The console. count() method logs the number of times that this particular call to count() has been called.

How many times is a function called?

Infinitely. Not really until you don't overflow the stack with function calls. Since each time a function is called all the variables used need space to be store in stack and stack is of limited size so someway in middle of any hundredth or thousandth call you will run out stack for function call.


1 Answers

You could get the time in milliseconds from the date object.

var start = new Date().getTime();

A();

function AJAXSuccessFunction() {
    console.log(new Date().getTime() - start);
}
like image 81
Will Avatar answered Oct 20 '22 00:10

Will