4

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<lanes.length;i++)
    htmlContents += GetLaneInfo($(this).find("Lane[num='"+lanes[i]+"']").attr('avg'));

I believe the slowest part of this code is the Lane[num=X] selector, how can I improve the performance of this? Can I cache $(this).find("Lanes") and then search through them later on?

XML Sample:

<Data time="10:50">
    <Lane num="102" avg="2.0"/>
    <Lane num="103" avg="2.0"/>
    <Lane num="104" avg="2.0"/>
    <Lane num="112" avg="2.0"/>
    <Lane num="113" avg="2.0"/>
    <Lane num="114" avg="2.0"/>
    <Lane num="115" avg="2.0"/>
    <Lane num="122" avg="0.9"/>
    <Lane num="123" avg="1.0"/>
    <Lane num="124" avg="1.0"/>
    <Lane num="132" avg="0.7"/>
    <Lane num="134" avg="0.7"/>
    <Lane num="142" avg="0.8"/>
    <Lane num="153" avg="0.4"/>
    <Lane num="154" avg="0.6"/>
</Data>
Chris
  • 26,744
  • 48
  • 193
  • 345

4 Answers4

2

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.

Chris
  • 26,744
  • 48
  • 193
  • 345
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • This would work great but, not all the lanes are always present in the XML, there may be gaps i.e. 1,2,3,6,7,8 etc. This wont work under this scenario will it? – Chris Jan 31 '12 at 16:23
  • 1
    @chris can i write an answer tomorrow ? I'll fix it and send you a message. – Royi Namir Jan 31 '12 at 16:39
1

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

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

enter image description here

Skyrim
  • 865
  • 5
  • 6
1

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.

Louis Ricci
  • 20,804
  • 5
  • 48
  • 62
0

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 :)

IProblemFactory
  • 9,551
  • 8
  • 50
  • 66