I attached my JavaScript snippet which can measure the internet speed. By sending a simple GET request to google.com each time, I able to receive a 200 OK response header. To overcome CORS, I run the script via console at the same tab where google.com was opened. I explain everything in the code section. But when I compare my output with services like fast.com and Ookla, It showing a huge difference. So kindly explain why this is happening? I'm I right or wrong? what about my JS script?
I attached the images of my output and the fast.com output.
//Internet speed calculation with JS ajax
function fire(){
//Get the request initiate time
var start = new Date().getTime();
/* Each and every request should be unique.
Stop getting resource from browser cache */
var val = Math.random();
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
//If request successfully completed and ready to serve
if(this.readyState==4 && this.status==200){
//Get the time when request completed
var end = new Date().getTime();
//mesure time taken for a request Round-trip in seconds
var diff = (end - start)/1000;
//Get content length from request header in bytes and convert it to KB
let content = xhr.getResponseHeader("content-length")/1024;
//The print output statement
console.log("Request length : "+content.toFixed(2)+" Kb "+"completed in "+diff.toFixed(2) +" seconds...");
}
}
//Send GET request while open google.com opened in browser to bypass CORS
xhr.open("GET","https://www.google.com?cache="+val,true);
xhr.send();
}
//Winin 1 sec delay send request and clear after 10 sec
var timer = setInterval(fire, 1000);
setTimeout(function() { clearInterval(timer);}, 10000);type here
The output of my script
So 465.79 kb transferred in 3.48 sec
The output of fast.com
fast.com showing different statistics !
Note - Please do not suggest any 3rd party libraries, pre-built solution and services to complete the task. I already tried existing stackoverflow solution in my case.
//Solution from Stackoverflow
//Show speed of 5.01Mbps
let imageURL = "https://sample-videos.com/img/Sample-jpg-image-5mb.jpg";
let sizeinBytes = 5266467;
let start, end;
let cache = "?rand=" + Math.random();
let image = new Image();
image.onload = function(){
end = (new Date()).getTime();
calculate();
};
start = (new Date()).getTime();
image.src = imageURL + cache;
function calculate(){
timeDiffInSec = (end - start)/1000;
let bits = sizeinBytes * 8;
let Bps = (bits / timeDiffInSec).toFixed(2);
let Kbps = (Bps / 1024).toFixed(2);
let Mbps = (Kbps / 1024).toFixed(2);
alert("Speed is : "+Mbps+" Mbps");
}
//Slightly modified code of mine
//Show speed of 1.01 Mbps
let start = new Date().getTime();
let val = Math.random();
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if(this.readyState==4 && this.status==200){
let end = new Date().getTime();
let diff = (end - start)/1000;
let contentinBytes = xhr.getResponseHeader("content-length");
let bits = contentinBytes * 8;
let Bps = (bits / diff).toFixed(2);
let Kbps = (Bps / 1024).toFixed(2);
let Mbps = (Kbps / 1024).toFixed(2);
alert("Speed is : "+Mbps+" Mbps");
}
}
xhr.open("GET","https://www.google.com?cache="+val,true);
xhr.send();
But fast.com says 23Mbps, How it is possible ?