JavaScript:String和Array上indexOf方法的效率差异 [英] JavaScript: difference in efficiency of indexOf method on String and Array

查看:564
本文介绍了JavaScript:String和Array上indexOf方法的效率差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇 indexOf 方法的效率是否存在差异,该方法可用于数组和JavaScript中的 String 。我认为 indexOf 在String上的效率低于在Array上的效率,而我的 new 测试结果支持这一点。例如:

I am curious whether there exists a difference in efficiency for the indexOf method that is available for both Array and String in JavaScript. I thought the indexOf is less efficient on String than on Array, and my new testing results support this. For example:

var arr = ['abc', 'ab', 'abz', '1'];

var str = 'abcababz1';

var needle = 'abxx';

//concatenate to make them bigger
for (var i = 0; i < 30; i++) {
    arr = arr.concat(arr);
    str = str.concat(str);
}
arr.push(needle);  //append needle last
str = str.concat(needle);

然后我使用了开始和结束时间戳

Then I used the start and end timestamp for

arr.indexOf(needle); // faster!
str.indexOf(needle); 

我在节点做了这个测试,新的测试结果显示:

I did this testing in node, the new testing results showed:

time used on Array is: 35
time used on String is: 57

因此,Array对indexOf比String更有效。这个新的测试基本上会创建最坏的情况 - 在String或Array的最后一针。

So the Array is more efficient for indexOf than String. This new testing basically creates the worst case scenario -- needle at the very end of String or Array.

编辑:

如果 indexOf 在Array上效率更高,我想知道我们是否应该首先拆分一个String(例如用逗号分隔)在使用 indexOf 方法搜索子字符串之前到数组。

If the indexOf is more efficient on Array, I was wondering if we should first split a String (delimited by comma, for example) to an array before using the indexOf method to search for a sub string.

对于此字符串:

var str2 = "hello,world,country,continent,ocean"

如果你搜索 ocean ,你会先拆分字符串 str2 到数组然后使用 indexOf 来查找 ocean

If you search for ocean, would you first split the string str2 to an array then use indexOf to find ocean?

var arr2 = str2.split(",");
arr2.indexOf('ocean');


推荐答案

我猜是根据你想要的编辑使用indexOf检查列表中是否存在以字符串形式出现的给定元素。

I'm guessing based on your edit that you want to use indexOf to check if a given element exists in your list that starts as a string.

然后两个选项是在字符串本身上使用indexOf或首先将其解析为数组并查看该元素是否存在,因为您知道格式为item1, item2。

The two options then are using indexOf on the string itself or parsing it to an array first and seeing if the element exists there, since you know the format is "item1,item2".

http:/ /jsperf.com/indexof-array-vs-string-efficiency

基于jsperf我们可以看到,即使indexOf在数组本身上更快,将字符串转换为数组也有成本,你最好在原始字符串上做indexOf。

Based on that jsperf we can see that even though indexOf is faster on the array itself, converting the string to an array has a cost to it as well and you're better off doing the indexOf on the raw string.

*注意String indexOf会需要一些额外的修改,以确保如果你有像blueocean这样的元素,indexOf(ocean)不会返回true,并且可能会想要indexOf(,ocean,)

*Note that the String indexOf would need some additional modification to make sure indexOf("ocean") doesn't return true if you have an element like blueocean, and would probably instead want indexOf(",ocean,")

这篇关于JavaScript:String和Array上indexOf方法的效率差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆