0

I will have to call below function multiple times in order to display dynamic data (while displaying a graph). This function will be called like more than 50 times, for displaying a single graph page. Problem is it takes long time to parse xml and display graph page (like more than a minute).

If size of file needs to be taken into consideration. First xml file to be parsed is 140kb. Second xml is 8kb.

May I know where I am going wrong? and how I could improve performance in this scenario. Would appreciate any input.

function map(md, tls) {
        
        //First xml file parsing    
        var melXmlFile = "xml/Mel.xml"
        xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET", melXmlFile , false);
        if (xmlhttp.overrideMimeType) {
            xmlhttp.overrideMimeType('text/xml');
        }
        xmlhttp.send();
        xmlDoc = xmlhttp.responseXML;
        .....

        //Second xml file parsing
        var msXmlFile = "xml/Ms.xml"
        xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET", msXmlFile, false);
        if (xmlhttp.overrideMimeType) {
            xmlhttp.overrideMimeType('text/xml');
        }
        xmlhttp.send();
        xmlMsDoc = xmlhttp.responseXML;
        ......
 }
     
   

Or should I be using different parsing style instead of XPath, so that performance is better? Thank you.

Sarahrb
  • 407
  • 11
  • 24
  • It is not clear whether you need to make the HTTP requests on each function call or just have to make the XPath evaluation based on the function parameters. Do you expect the documents to change between function calls? – Martin Honnen Nov 28 '22 at 13:20
  • No, I do not expect documents to change between function calls. I will have to parse xml files based on function parameters. Just extract required data from xml and return them as shown in last couple of lines. – Sarahrb Nov 28 '22 at 13:29
  • 2
    Then I would consider making the two HTTP request exactly once, the first time you need to extract data, and store them in a variable or two, so that subsequential calls just need to make the XPath evaluation. The other code is hard to optimize, first of all the performance of XPath evaluation will differ between engines, and you haven't posted the XML sample structure and the requirements in plain English to allow us to suggest easier/more direct XPath evaluation approaches. – Martin Honnen Nov 28 '22 at 13:34
  • I notice you declare a local function variable `var msNode = [];` and set `msNode.push(colorTextNode.textContent)`, but otherwise the `msNode` array isn't used, it is certainly not returned by the function. So based on reading that code it seems you don't need those lines. – Martin Honnen Nov 28 '22 at 13:35
  • 1
    @Sarahb, since you asked me directly, I agree with Martin Honnen: parse the XML documents once, store them as such, and then just do your XPath queries when required to display your graph – julien.giband Nov 28 '22 at 13:41
  • So, I should be making http request before the func call and store them in a variable and pass that variable (for eg: xmlDoc ) as parameter to the function. So that it makes only two HTTP request. I will try that. Thank you both. – Sarahrb Nov 28 '22 at 13:45
  • Thank you. I do use msNode, and return a value based on that. I have excluded few lines for simplicity. – Sarahrb Nov 28 '22 at 13:49
  • That sample document in https://stackoverflow.com/questions/74388009/accessing-nested-xml-going-upwards doesn't have any `Elnic` or `Modes` or `Modes` nor `Command` elements so I don't see how it would be an input sample for any of the two of your JavaScript code sections. – Martin Honnen Nov 28 '22 at 15:14
  • @Marin Honnen Following as you mentioned (consider making the two HTTP request exactly once) improved performance drastically. Please add it as answer. Thanks a lot. – Sarahrb Nov 28 '22 at 15:28

1 Answers1

1

In every function call you make two new XMLHttpRequests to load and parse the two XML input documents. As you said in a comment that the documents don't change in between calls to the function you can certainly improve things by doing the two requests only once and store the two documents and simply have your function perform the XPath evaluation on the stored documents.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110