Can you know the response time of an AJAX request with jQuery?

Question:

I wanted to know if there is any function that returns the AJAX request time in microseconds, because I need to get this value for the project I'm developing.

Answer:

You need to keep a timestamp from the time of the request, get another one at the time of the response, and compare the two:

var inicio = performance.now();
$.get('https://httpbin.org/get').done( function(response) {
    var tempo = performance.now() - inicio;
    console.log('A requisição levou ' + tempo + 'ms');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

The time is in milliseconds, but note that it has decimal places. This is only possible with performance.now() , which is not supported in older browsers, instead of Date.getTime() , which has no support issues.

Scroll to Top