js中for循环和indexOf()性能对⽐
1、js 中for循环和indexOf()性能对⽐
在js中提供了indexOf()函数以获取某个字符在字符串中的index,可以通过它也判断某个字符或字符串是否存在。
但同时在js中for循环也可以实现同样的效果(判断字符是否存在)。那么到底⽤哪个才好呢?
于是写了两个函数来⽐较:
function getValue() {
var str = "abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghija  var specialWord = new Array("'", "\\", "<", ">", "%", "?", "/", "+", "@", "&", "#", "$", "……", "^", "~", "!", "‘", "’", "!", "¥"); //可以继续添加特殊字符此 /  字符也不可输⼊输出时会破
坏JSON格式
for (var specialWordItem = 0; specialWordItem < specialWord.length; specialWordItem++) {
ar specialIndex = str.indexOf(specialWord[specialWordItem]);
if (specialIndex != (-1)) {
return false;
}
}
return true;
}
function getReValue() {
var str = "abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghija  var specialWord = new Array("'", "\\", "<", ">", "%", "?", "/", "+", "@", "&", "#", "$", "……", "^", "~", "!", "‘", "’", "!", "¥"); //可以继续添加特殊字符此 /  字符也不可输⼊输出时会破
坏JSON格式
for (var specialWordItem = 0; specialWordItem < specialWord.length; specialWordItem++) {
for (var j = 0; j < str.length; j++) {
if (specialWord[specialWordItem] == str.charAt(j)) {
return false;
}
}
}
return true;
}
unction bothFunction() {
getValue();
getReValue();
}
str字符串是1000个字符。判断str中是否存在specialWord中的特殊字符。性能测试结果如下图所⽰:
现在可以很明显地看到⽤indexOf()性能要好很多。
2、js中的for循环写法的效率对⽐
⼀共三种写法如下:
for (var i = 0; i < arr.length; i++)
for (var i in arr)
代码运行js特效for (var i = 0, len = arr.length; i < len; i++)
先看下运⾏测试的界⾯:
再看下对应的三个结果,三种写法执⾏的效率是有很⼤不同的:
如此看来,执⾏效率最快的是第三种:
for (var i = 0, len = arr.length; i < len; i++)
先声明变量,再进⾏循环判断,效率远⽐遍历数组⾼得多,也⽐循环取得数组长度的效率快得多!下⾯是页⾯源代码:
<html>
<body>
<script type="text/⽹页特效">
function makeArr(num) {
var arr = [];
for (var i = 0; i<num; i++){
arr.push('abc');
}
arr.join('');
return arr;
}
var num = 10000000;
function function1() {
var arr = makeArr(num);
var start = (new Date()).valueOf();
var count = 0;
for (var i = 0; i < arr.length; i++) {
count++;
}
var end = (new Date()).valueOf();
console.log('Count: '+count+' times');
console.log('Time spent: '+(end - start));
}
function function2() {
var arr = makeArr(num);
var start = (new Date()).valueOf();
var count = 0;
for (var i in arr) {
count++;
}
var end = (new Date()).valueOf();
console.log('Count: '+count+' times');
console.log('Time spent: '+(end - start));
}
function function3() {
var arr = makeArr(num);
var start = (new Date()).valueOf();
var count = 0;
for (var i = 0, len = arr.length; i < len; i++) {
count++;
}
var end = (new Date()).valueOf();
console.log('Count: '+count+' times');
console.log('Time spent: '+(end - start));
}
</script>
for (var i = 0; i < arr.length; i++) <br />
<button onClick="function1();">loop 1</button><br /> <p id="f1"></p>
for (var i in arr)<br />
<button onClick="function2();">loop 2</button><br /> <p id="f2"></p>
for (var i = 0, len = arr.length; i < len; i++)<br />
<button onClick="function3();">loop 3</button><br /> <p id="f3"></p>
</body>
</html>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。