I am working on detecting the connection speed, so i planned to go with window.performance object for duration calculation.
I am little confused with window.performance.timing object is generated based on the whole page load, or based on the last request and response.
For Example:
I am having 5 server call for web page load, performance.timing
object is generated based on all the 5 server calls or based on the 5th server call(last call).
sample connection speed calculation for reference
var bitsLoaded = 100000; //bits total size of all files (5 server call). var duration = performance.timing.responseEnd - performance.timing.navigationStart; var speedBps = Math.round(bitsLoaded / duration); var speedKbps = (speedBps / 1024).toFixed(2); var speedMbps = (speedKbps / 1024).toFixed(2);
Anything not clear ready to explain
Any idea about window.performance
performance. The Window interface's performance property returns a Performance object, which can be used to gather performance information about the current document.
In JavaScript performance. now() method can be used to check the performance of your code. You can check the execution time of your code using this method. It returns the value of time (of type double) in milliseconds. The returned value represents the time elapsed since the execution started.
Web performance refers to the speed in which web pages are downloaded and displayed on the user's web browser.
The performance. now() method returns a DOMHighResTimeStamp , measured in milliseconds. The returned value represents the time elapsed since the time origin.
Not sure if this chart gives you a better understanding of performance.timing.
For your question:
I am having 5 server call for web page load, performance.timing object is generated based on all the 5 server calls or based on the 5th server call(last call).
The answer is: performance.timing
is generated based on all requests and responses (but not including the ajax ones).
For the sample connection speed calculation script you gave, I guess the below one is better.
var duration = performance.timing.responseEnd - performance.timing.responseStart;
The reason is: the duration from navigationStart
to responseEnd
includes DNS timing which does not transfer any data from server to client.
Please refer to https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/NavigationTiming/Overview.html for the definition of timings.
performance.timing is NOT based on the last request and response. It measures the delay between navigationStart stage and loadEventStart stage in a page life cycle. As explained here, it's main use is to determine the Page Load Speed.
In my opinion. performance.timing can't really be used for measuring connection speed since it's initiated After the user has received the response and is a javascript object, which runs on the browser. I suggest you simply ping the user and use the round trip time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With