Javascript parsing XML performance



I am having trouble with the performance of a mobile application, and would like to ask for alternate, more performance-minded ways of doing the things I am already doing. I am currently parsing a shallow XML document that is in this format:



<root>
<Row>
<Tab>data1</Tab>
<Category>data2</Category>
<jan_2013>...</jan_2013>
<feb_2013>...</feb_2013>
...
<ytd_2013>...</ytd_2013>
<jan_2014>...</jan_2014>
...
<ytd_2014>...</ytd_2014>
</Row>


If you do the math, there are 28 elements in the Row element (which will grow another 13 as we enter 2015). And there are currently 520 Rows in the document, so overall ~14,500 pieces of data. I am initially loading the XML through an AJAX call:



$.ajax({
type: "GET",
url: "data.xml",
cache: false,
dataType: "xml",
success: function(xml) {
...
},
error: function(xhr, textStatus, error){
console.log(xhr.statusText);
console.log(textStatus);
console.log(error);
}
});


And to find the data I need, I iterate over the XML using JQuery:



function searchXML(xml, goalTab, goalCategory){
$(xml).find('Row').each(function(){ // for each Row in xml
var row = this;
var boolTab = $(row).find('Tab').text() == goalTab;
var boolCategory = $(row).find('Category').text() == goalCategory;

if (boolCategory && boolTab) {
//return data


This work is for a mobile application. To load the page that contains this Javascript takes upwards of 10 seconds, and I have 7 separate HTML pages that load the XML.


I asked a coworker, and he suggested my problem with the AJAX call, and that I should try and rework it.


I also looked into using a cookie but it seems from the different sources I've seen that cookies are for smaller, more succinct pieces of data as opposed to entire data documents because a Javascript cookie is essentially a string. Also, I would need to load in some sort of multidimensional array that would contain the data itself as well as the tag name as well as separate the data by Rows.


Is there something I can do to the existing functions above to improve performance? Or is there a different way of storing the XML other than a cookie?


No comments:

Post a Comment