//todo function base64Encode
let d = {};
setTimeout(function(){
let count = 0;
let time = 0;
let loadedCount = 0;
(function send() {
if (count > 10) {
return;
}
let img = document.createElement("img");
let t = +(new Date);
img.src = "http://asdf.com/?d=" + base64Encode(JSON.stringify(d)) + "&t=1&r=" + Math
.random();
img.onload = img.onerror = () => {
let diff = +(new Date) - t;
time += diff;
console.log("img",time,count,time/count);
}
document.body.appendChild(img);
countPP;
send();
})();
},0);
setTimeout(function () {
let count = 0;
let time = 0;
let loadedCount = 0;
function send() {
if (count > 10) {
return;
}
let script = document.createElement("script");
let t = +(new Date);
script.type="text/javascript";
script.src = "http://asdf.com/?d=" + base64Encode(JSON.stringify(d)) +
"&t=1&r=" +
Math.random();
script.onload = script.onerror = (e) => {
let diff = +(new Date) - t;
time += diff;
console.log("script",time,count,time/count);
}
document.body.appendChild(script);
countPP;
send();
};
send();
}, 200);
the above code sends an get request to an address using img and script, respectively. The page has been refreshed many times, and 90% of the time the result is that script is faster than img, and the personal estimate is that it takes about 20% less time.
as shown in figure
Why is this?