Sunday, 15 May 2011

google chrome - javascript indexOf with millions of matches -


I am trying to remove some lines that represent some XML elements from a file. The user has a simple & lt; Input type = "file" & gt; Provides a file using tag, and is read as text with this file from FileReader , and given in the form of the parameter:

gt;" "end": "& lt; / header>"}, {"start": "& lt; Lt; someElement & gt; "," end ":" & lt; / someElement & gt; "}]; Function Deal With String (Invalid XML) {var validXML = ""; For (var i = 0; i & lt; contextual delimiter height; i ++) {delimiter = relevantDelimiters [i]; While (invalidXML.indexOf (delimiter.begin)! = -1) {// While the relevant elements of this type are left: startPos = invalidXML.indexOf (delimiter.begin); EndPos = invalidXML.indexOf (delimiter.end); // Add to finish the result: Valid XML + = invalidXML.substring (startPos, endPos + delimiter.end.length) + "\ n"; // invalidXML = invalidXML.replace (invalidXML.substring (startPos, endPos + delimiter.end.length), "") to work on the next item to move this item out of the input; }} // Return Fixed data returns valid XML; }

This approach works fine with few matches in the input text file, but a file of 1.5 MB is given, the script has been stuck (with Google Chrome running) , Tab to make it non-responsive) There are approximately one million "relevant elements" in this file, which means the corresponds to the relevant delimiter .

How can I customize it?

Replace "code" instead of "code moving it out of the input" Instead of calling you, you use the second argument: fromIndex . In this way, it searches for the next spot after the given index, and you can loop through a very large input without the need to touch it.

  function dealWithString (invalidXML) {var validXML = ""; {Var delimiter = relatedDelimiters [i], pos = 0, startPos; for var i = 0; i & lt; contextual delimiter lamps; i ++) While ((startPos = invalidXML.indexOf (delimiter.begin, pos))! = -1) {// The relevant elements of this type of left are: var endPos = invalidXML.indexOf (delimiter.end, startPos); // Insert (And Piece! = -1) - Otherwise this could be a very bad situation = Endos + delimiter.and.line; // Attach to finish the result: Valid XML + = invalidXML.slice (startPos, pos) + "\ n"; }} Valid XML return; }  

No comments:

Post a Comment