I have a server where quickfire requests are sent to it from a static app. I previously used fetch to get data from the server, but noticed that is was very slow, often delaying the service by 60ms per request. I need the text from the server. My server is hosted on a free plan, so it goes to sleep. I have to send a http request to my host to start it up, which takes a few seconds. I don't mind the few seconds of loading time, since the server only sleeps after an hour of inactivity, but the delay of 60ms per request really slows the app down. I have been doing this (I cant do await on static apps and I cant do async functions because then I would need to rewrite a lot of stuff in my app):
fetch('https://server.freenodejshost.com').then(r=>{
r.text().then(txt=>{
//do something with the info
})
})
Is there any way to use fetch faster? Or is there a faster alternative to fetch?
Check out the benchmark test below. XHR works faster than fetch in most cases.
XHR:
const xhr = new XMLHttpRequest();
xhr.open('GET', 'https://www.googleapis.com/discovery/v1/apis');
xhr.onload = () => console.log(JSON.parse(xhr.responseText));
xhr.send();
fetch:
fetch('https://www.googleapis.com/discovery/v1/apis').then(response => response.json()).then(console.log)