[英]What is the most efficient way to go through string's chars in Javascript?
I saw there are similar questions on SO but I didn't found one which matches my specific goal. 我看到在SO上也有类似的问题,但没有找到一个符合我特定目标的问题。
I have developed a small function which loops through a string (let's say between 10 and 160 chars) to extract 我开发了一个小功能,它可以循环通过一个字符串(假设在10到160个字符之间)来提取
Example of string to parse: "word1 word2 $1 word3 $2 word4 $3". 要解析的字符串示例:“ word1 word2 $ 1 word3 $ 2 word4 $ 3”。
Example of output: 输出示例:
{
strings: [word1 word2, word3, word4],
specialChars: 3
}
For sure there are also the limit cases (no wildcards, 1 wildcard only, wildcard can be at the beginning or end of the string). 当然,也存在极限情况(无通配符,仅1个通配符,通配符可以位于字符串的开头或结尾)。
The algorithm I wrote so far is as follows (there is something to fix but I am more interested in the efficiency than then result, in this question): 到目前为止,我编写的算法如下(有一些问题需要解决,但在这个问题上,我对效率的关注比对结果的兴趣更大):
function parsestr(str: string) {
let specialChars = [],
plaintextParts = [],
resultObject = {
strings: [],
specialChars: 0
};
// convert to char array and loop
str.split('').forEach(function(c, i){
if (c=='$') specialChars.push(i);
});
resultObject.specialChars = specialChars.length;
// extract plain text parts
if (specialChars.length == 0) return resultObject;
// part before first wildcard
if (specialChars[0]>0) plaintextParts.push(str.substr(0, specialChars[0]-1));
// 2..N
for (var i=1; i<specialChars.length-1; i++) {
plaintextParts.push(str.substr(specialChars[i-1]+2, specialChars[i]-1));
}
// last part
if (specialChars[specialChars.length-1]+1 < str.length-1)
plaintextParts.push(str.substr(specialChars[specialChars.length-2]+2, specialChars[specialChars.length-1]-1));
resultObject.strings = plaintextParts;
return resultObject;
}
// call function and print output
var jsonString = "word1 word2 $1 word3 $2 word4 $3";
console.log(JSON.stringify(parseJsonString(jsonString)));
You can just use a regex: 您可以只使用正则表达式:
let str = "word1 word2 $1 word3 $2 word4 $3";
let regex = /\$[0-9]/;
let strSplit = str.split(regex);
console.log(JSON.stringify(strSplit)); // ["word1 word2 ", " word3 ", " word4 ", ""]
If you do not want that last empty string you can always just s(p)lice it off. 如果您不希望最后一个空字符串,可以随时将其关闭。
So if you want the number of "special chars" you can just take the length of the result strSplit
and subtract one. 因此,如果您想要“特殊字符”的数量,则可以只取结果
strSplit
的长度,然后减去一个。
In terms of efficiency, this seems like a micro-optimization, especially for strings no longer than 160 chars. 在效率方面,这似乎是微优化,尤其是对于不超过160个字符的字符串。 It's more important to make your code work in a clean, understandable way than to optimize the efficiency of each operation.
使代码以一种清晰易懂的方式工作比优化每个操作的效率更为重要。
When it comes to optimizing your code, try to make your general solution more efficient, and then optimize specifics later. 在优化代码时,请尝试使通用解决方案更有效,然后在以后优化细节。 Assuming that this function doesn't exist in a vacuum, try focusing on the performance of app itself and figure out where the actual performance bottlenecks are before optimizing something like string manipulation.
假设此功能不是凭空存在的,请在优化诸如字符串操作之类的内容之前,着重研究应用本身的性能并找出实际性能瓶颈在哪里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.