AJAX Call Processing Time Measurement

I want to measure how long it takes to process an AJAX call . I run it, but don't know how to code it in javascript (js only)

+12
javascript
Mar 03 '11 at 6:30
source share
2 answers
var start = new Date().getTime(); doAjax({ success: function() { var end = new Date().getTime(); console.log('milliseconds passed', end - start); } }); 

save the time value before you start making ajax material, and then do it again when you're done. Then subtract one form from the other and get your time.

+24
Mar 03 2018-11-11T00:
source share

This will not give exact timings because javascript uses an event queue . This means that your program can run as follows:

  • Run an AJAX request
  • Handle the pending mouse click event / any other code wait string in the meantime
  • Start processing an AJAX response

Unfortunately, there is no way to get the time when the event was added to the queue, as far as I know. Event.timeStamp returns the time when the event was pushed out of the queue, see this script: http://jsfiddle.net/mSg55/ .

Html:

 <a href="#">link</a> <div></div> 

JavaScript:

 $(function() { var startTime = new Date(); $('a').click(function(e) { var endTime = new Date(e.timeStamp); $('div').append((endTime - startTime) + " "); //produce some heavy load to block other waiting events var q = Math.PI; for(var j=0; j<1000000; j++) { q *= Math.acos(j); } }); //fire some events 'simultaneously' for(var i=0; i<10; i++) { $('a').click(); } }); 
+3
Jul 03 '14 at 10:42 on
source share



All Articles