简体   繁体   English

在JavaScript中过滤字符串数组的最快方法

[英]fastest way to filter array of strings in JavaScript

I am working with some xml files stored on S3. 我正在使用S3上存储的一些xml文件。 I use the xml2js module in Node to parse the xmls and then I extract the strings that have .jpg in them. 我在Node中使用xml2js模块来解析xml,然后提取其中包含.jpg的字符串。 I was using the filter method and then tried using my own for loop but that didn't shave any time off. 我使用了filter方法,然后尝试使用自己的for循环,但这并没有节省任何时间。 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: 使用for循环:

//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('"'); 使用file.split('“')之后,数据如下所示:

[ '{','rstuv',':{','options',':[{','![alt](CKrgUgiYMflaWnsGZ009.jpg)']];
 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. 字符串化然后搜索该字符串不仅会给您带来很多错误(当字符串确实包含引号时),许多数组元素甚至都不是正确的字符串,与仅将原始XML文件作为文本读取相比,这几乎没有任何改进。并直接搜索。

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. 相反,只需遍历(或在必要时进行递归) data对象(有关详细信息,请参阅访问/处理(嵌套)对象,数组或JSON ),然后在期望的位置过滤字符串(和属性名称?)。 This will be a lot faster. 这会快很多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM