Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get actual roundtrip-time in browser using JavaScript

A simple method to measure the roundtip-time of an Ajax request is to measure the time between the start and the end of a request (readyState 4). Many examples exist for this kind of measuring.

But this measurement is not really accurate. The Ajax callback will only be invoked, as soon as it comes up in the browser event loop. Which means that if there is some blocking operation inbetween, the measurement would also contain some client processing time and not the actual server roundtrip-time (server processing time + network time).

I know that this kind of functionality is now available through the Resource Timing API Specification, but at the moment it is not consistently implemented accross all browsers.

Is there any other way to find out the real roundtrip-time or the timestamp, at which the server response is available and waiting for the corresponding callback to execute?

like image 511
ppa Avatar asked Nov 17 '25 20:11

ppa


1 Answers

NetworkInformation.rtt

You can use navigator.connection.rtt to obtain the estimated effective round-trip time of the current connection, rounded to the nearest multiple of 25 milliseconds.

const estimated_round_trip_time = navigator.connection.rtt;

console.log(estimated_round_trip_time);

Note: At the time of posting, navigator.connection.rtt is still considered experimental technology. Expect behavior to change in the future, and proceed with caution before using it in your application. See Browser Compatibility.

like image 84
Grant Miller Avatar answered Nov 20 '25 11:11

Grant Miller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!