Reputation: 538
I am working with some xml files stored on S3. I use the xml2js module in Node to parse the xmls and then I extract the strings that have .jpg in them. I was using the filter method and then tried using my own for loop but that didn't shave any time off. Is there any faster way to write this section of the code or is this the fastest way to get it done. Any help appreciated.
using filter method:
//this took 52393ms
var file = JSON.stringify(data);
var arrayOfStrings = file.split('"');
var images = arrayOfStrings.filter(function(str) {
return str.indexOf('.jpg') !== -1;
});
resolve(images);
using for loop:
//this took 52681ms
var file = JSON.stringify(data);
var arrayOfStrings = file.split('"');
var images =[];
for(let i = 0; i < arrayOfStrings.length; i++) {
if(arrayOfStrings[i].indexOf('.jpg') !== -1) {
images.push(arrayOfStrings[i]);
}
}
resolve(images);
data looks like the following after I use file.split('"');
[ '{','rstuv',':{','options',':[{','![alt](CKrgUgiYMflaWnsGZ009.jpg)']];
Upvotes: 0
Views: 2237
Reputation: 664548
var file = JSON.stringify(data); var arrayOfStrings = file.split('"');
Don't do that. If you want to search through data, keep it structured. Stringifying and then searching that string will not only get you a bunch of mistakes (when strings did contain a quote), lots of array elements that are not even proper strings, and is hardly any improvement over just reading in the original XML file as text and directly searching that.
Instead, just iterate (or recurse if necessary) through the data
object (see Access / process (nested) objects, arrays or JSON for details), and filter for the strings (and property names?) in those locations where you expect them. This will be a lot faster.
Upvotes: 5