Skip to content Skip to sidebar Skip to footer

Jquery - Improving Selector Performance When Processing Xml

I am processing an XML file that has very slow performance when selecting Nodes with XPath style selectors. Here is part of the code that runs particularily slow for (i=0;i

Solution 1:

try this :

http://jsperf.com/1f

Ive managed to increase the speed. enter image description here

p.s. it is based on the fact that all lanes are in the same order in each xml node.

Solution 2:

Using XML parsing for such simple markup is a waste. If you want speed using indexOf and substring is the superior method.

http://jsperf.com/1f/2

I edited @Royi Namir 's jsperf and added my own version (aptly named "screw xml"). It runs 2x faster than his optimized XML parsing version.

Here's the code that would aline with the OP's example from the question. The "xml" variable is just a string that represents the XML.

var find = '';
var start = -1;
var end = -1;
var skip1 = 0;
var skip2 = ' avg="'.length;
//
for (i=0;i<lanes.length;i++) {
  find = 'num="' + lanes[i] + '"';
  skip1 = find.length;
  end = -1;
  start = xml.indexOf(find, 0);
  while (start != -1) {
    start = start + skip1 + skip2;
    end = xml.indexOf("\"/>", start);
    htmlContents += GetLaneInfo(xml.substring(start, end));
    start = xml.indexOf(find, end);
  }
}

In reality you probably don't want to use a version like the above because it hinges on the XML being formatted uniformly (see: "skip2" variable/constant). But if you really want performance/speed doing it this way is the fastest by far.

Solution 3:

I know this is late, but here's a viable high performance solution:

http://jsperf.com/1f/3

enter image description here

Solution 4:

Well, in your example xml data we could see that num attribute is sorted, if so try implement http://en.wikipedia.org/wiki/Bisection_method for that data :)

Post a Comment for "Jquery - Improving Selector Performance When Processing Xml"