JQuery - Improving selector performance in XML processing

I process an XML file with very poor performance when selecting nodes using XPath style selectors.

Here is a piece of code that runs especially slowly

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? Is it possible to cache $(this).find("Lanes") and then search through them?

XML example:

 <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> 
+4
source share
4 answers

try the following:

http://jsperf.com/1f

I managed to increase the speed. enter image description here

ps it is based on the fact that all bands are arranged in the same order in each xml node.

+2
source

Using XML parsing for such simple markup is a waste. If you want to speed up the use of indexOf and substrings .

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

I edited @Royi Namir jsperf and added my own version (aptly named "screw xml"). It runs 2 times faster than its optimized version of XML parsing.

Here is code that does not match the OP example from the question. The variable "xml" is just a string representing 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 fact, you probably do not want to use a version similar to the one above, because it depends on the format evenly formatted (see "skip2" variable / constant). But if you really want performance / speed to run like this, this is the fastest.

+1
source

I know this is already late, but there is a viable high-performance solution here:

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

enter image description here

+1
source

Well, in your xml example, we saw that the num attribute is sorted if this is an attempt to implement http://en.wikipedia.org/wiki/Bisection_method for this data :)

0
source

Source: https://habr.com/ru/post/1393962/


All Articles