简体   繁体   English

为什么 Set.prototype.has() 比 Array.prototype.includes() 快得多? 节点 v10

[英]Why is Set.prototype.has() much faster than Array.prototype.includes()? Node v10

I have two arrays of strings, both having a length of ~150000.我有两个字符串数组,长度都为 ~150000。 I wanted to find shared elements between the two, so I used filter() on array1, and for each element checked if array2.includes[array1element].我想找到两者之间的共享元素,所以我在 array1 上使用了 filter(),并检查每个元素是否 array2.includes[array1element]。 this was taking around 1.5 mins to finish executing, however when I changed the arrays to sets, and used set2.has(set1element), it executed in <1s to filter.这大约需要 1.5 分钟才能完成执行,但是当我将数组更改为集合并使用 set2.has(set1element) 时,它会在 <1 秒内执行以进行过滤。 I spread the contents of set1 into an array, but searching is being done on set2 using Set.prototype.has().我将 set1 的内容展开到一个数组中,但是使用 Set.prototype.has() 在 set2 上进行搜索。

I'm new to sets, I actually just discovered them because arrays were taking so long to search through and I was looking for alternatives.我是集合的新手,实际上我才发现它们是因为数组需要很长时间来搜索,而我正在寻找替代方案。 Can someone explain why there is such a significant time difference?有人可以解释为什么会有这么大的时差吗? I don't have much of a maths background so answers with less mathematical/algorithmic jargon are appreciated!我没有太多的数学背景,所以对数学/算法术语较少的答案表示赞赏!

CODE: where wireBpath and wireApath are arrays of strings: runtime ~1.5mins代码:其中wireBpath 和wireApath 是字符串数组:运行时间~1.5 分钟

let intersections = wireBpath.filter(position => wireApath.includes(position));

where wireBpath is an array(spread from a set to use the filter() method), and wireApath is a set: runtime<1s其中,wireBpath 是一个数组(从集合传播以使用 filter() 方法),wireApath 是一个集合:runtime<1s

let intersections = wireBpath.filter(position => wireApath.has(position));```


That is because the time complexity is different for Set and Array那是因为SetArray the time complexity不同

The methods used by Sets to search for items has a time complexity of just O(1) , but the methods an array uses to search for items has a linear time complexity of O(N) . Sets 用于搜索项目的方法的时间复杂度仅为O(1) ,但数组用于搜索项目的方法具有O(N)的线性时间复杂度。 In other words, the run-time increases at the same rate as the size of the data increases.换句话说,运行时间随着数据大小的增加而增加。

For more details you can see this article有关更多详细信息,您可以查看这篇文章

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

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