jsindexOf()和for循环的执⾏效率那个更⾼
在js中供给了indexOf()函数以获取某个字符在字符串中的index,可以经由过程它也断定某个字符或字符串是否存在。
但同时在js中for轮回也可以实现同样的结果(断定字符是否存在)。那么到底⽤哪个才好呢?
于是写了两个函数来⽃劲:
View Code
1 <script type="text/javascript">
2 function getValue() {
3
4 var str = "abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdef ghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcd
efghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijab cdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefg hijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcde fghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij"
5 var specialWord = new Array("""", "\\", "<", ">", "%", "?", "/", "+", "@", "&", "#", "¥", "……", "^", "~", "!", "‘", "’","!", "¥"); //可以持续添加特别字符此 / 字符也不成输⼊输出时会破损JSON格局
6 for (var specialWordItem = 0; specialWordItem < specialWord.length; specialWordItem++) {
7 var specialIndex = str.indexOf(specialWord[specialWordItem]);
8 if (specialIndex != (-1)) {
9 return false;
10 }
11 }
12 return true;
13 }
14
15 function getReValue() {
16
17 var str = "abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdef ghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcd efghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijab cdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefg hijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcde fghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij"
18 var specialWord = new Array("""", "\\", "<", ">", "%", "?", "/", "+", "@", "&", "#", "¥", "……", "^", "~", "!", "‘", "’","!", "¥"); //可以持续添加特别字符此 / 字符也不成输⼊输出时会破损JSON格局
19 for (var specialWordItem = 0; specialWordItem < specialWord.length; specialWordItem++) {
20 for (var j = 0; j < str.length; j++) {
21 if (specialWord[specialWordItem] == str.charAt(j)) {
22 return false;
indexof的用法javascript23 }
24 }
25 }
26 return true;
27 }
28
29 function bothFunction() {
30 getValue();
31 getReValue();
32 }
33
34 </script>
str字符串是1000个字符。断定str中是否存在specialWord中的特别字符。机能测试成果如下图所⽰:如今可以很明显地看到⽤indexOf()机能要好很多。
当然getValue()还可以持续优化
View Code
function getValue(str, specialWord) {
if ((typeof str == "string") && (specialWord instanceof Array)) {
var specialIndex = -1,
specialWordLength = specialWord.length;
for (var specialWordItem = specialWordLength - 1; specialWordItem >= 0; specialWordItem--) {
specialIndex = str.indexOf(specialWord[specialWordItem]);
if (specialIndex > -1) {
return false;
}
}
}
return true;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论