1

I want to confirm if I am not slowing down the code below. My goal is to increase the performance of the application. I was considering a promise.all but not sure if it is necessary as I believe the code is written now, all the fetch requests are running simultaneously?

The test functions don't need to wait for each other. I don't want to create a funnel where each test function waits for the other one to finish or each fetch request is waiting for the previous one to finish. The goal is to have them all running together. Is my code doing that at the moment? Thank you for your assistance.



function test1() {
   //some code here

   fetch(URL)
   .then(checkStatusandContentType)
   .then(HtmlToObject)
   .then(subTest1=> //work with the data here)
    .catch(error => {console.log('Request failed', error);});
}

function test2() {
   //some code here

   fetch(URL)
   .then(checkStatusandContentType)
   .then(HtmlToObject)
   .then(subTest2 => //work with the data here)
    .catch(error => {console.log('Request failed', error);});

   fetch(URL)
   .then(checkStatusandContentType)
   .then(HtmlToObject)
   .then(subTest3 => //work with the data here)
    .catch(error => {console.log('Request failed', error);});

    fetch(URL)
   .then(checkStatusandContentType)
   .then(HtmlToObject)
   .then(subTest4 => //work with the data here)
    .catch(error => {console.log('Request failed', error);});
}

//....and hundreds like the above test functions down here

const checkStatusandContentType = async response => {
    const isJson = response.headers.get('content-type')?.includes('application/json');
    const isHtml = response.headers.get('content-type')?.includes('text/html');
    const data =  isJson ? await response.json() 
                : isHtml ? await response.text()
                : null;

    // check for error response
    if (!response.ok) {
        // get error message from body or default to response status
        const error = (data && data.message) || response.status;
        return Promise.reject(error);
    }
    return data;
}

const HtmlToObject =  data => {
    const stringified = data;
    const processCode = stringified.substring(stringified.lastIndexOf("<X1>") + 4, stringified.indexOf("</X1>"));  
    //CONTENT EXTRACT
    data = JSON.parse(processCode);
    return data;
};
O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • Yes, if those three URLs are different in some way, then your code is making three requests in parallel. You would add `Promise.all()` only if you wanted to know when all three were done. Keep in mind that how much this speeds things up will also depend upon how much the server can really handle three parallel requests and truly process them in parallel. – jfriend00 Feb 10 '22 at 05:13

1 Answers1

0

TL;DR fetch and XmlHTTPRequest perform the same.

You said you want to increase your application's performance. It's usually wise to dream up a way of measuring the performance when you do that.

Your performance measurement may be for just one desktop user with an excellent connection to the network. Or, it may be for hundreds of mobile devices using your app at the same time.

Browser HTML / Javascript apps using XmlHTTPRequest (XHR for short) or fetch requests are often designed to display something useful to your user, and then use the received data to make that display even more useful. If your measure of performance is how long it takes for a single user to see something useful, you may already have acceptable peformance. But if you have hundreds of mobile users, performance is harder to define.

You asked about whether XHR or fetch has better performance. From the point of view of your server, they are the same: both generate requests that your server must satisfy. They both generate the same requests; your server can't tell them apart. fetch requests are easier to code, as you have discovered.

Your code runs many requests. You showed us three but you said you have many more. Browsers restrict the number of concurrent outbound requests, and others wait for an available slot. Here's information about concurrent requests in an answer. Most browsers allow six concurrent requests to any given domain, and ten overall.

So, your concurrent fetch operations (or concurrent XHR operations, it doesn't matter which) will hit your server with six connections at once. That's fine for low-volume applications with good bandwidth. But if your app scales up to many users or must work over limited (mobile) bandwidth, you should consider whether you will overload your users' networks or your server.

Reducing the number of requests coming from your browser app, and perhaps returning more complete information in each request, is a good way to manage this server and network load.

O. Jones
  • 103,626
  • 17
  • 118
  • 172