JavaScript 对象与数组查找性能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26353417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:47:12  来源:igfitidea点击:

JavaScript object vs. array lookup performance

javascript

提问by

What is the performance difference between retrieving the value by key in a JavaScript object vs iterating over an array of individual JavaScript objects?

在 JavaScript 对象中通过键检索值与迭代单个 JavaScript 对象的数组之间的性能差异是什么?

In my case, I have a JavaScript object containing user information where the keys are the user's IDs and the values are each user's information.

就我而言,我有一个包含用户信息的 JavaScript 对象,其中键是用户的 ID,值是每个用户的信息。

The reason I ask this is because I would like to use the angular-ui-selectmodule to select users, but I can't use that module with a Javascript object - it requires an array.

我问这个的原因是因为我想使用该angular-ui-select模块来选择用户,但我不能将该模块与 Javascript 对象一起使用 - 它需要一个数组。

How much, if anything, am I sacrificing by switching from a lookup by key, to a lookup by iteration?

通过从按键查找切换到迭代查找,我牺牲了多少(如果有的话)?

By key:

按关键字:

var user = users[id];

By iteration

通过迭代

var user;

for (var i = 0; i < users.length; i ++) {
  if (users[i].id == id) { 
    user = users[i]; break;
  }
}

采纳答案by Ole Borgersen

The answer to this is browser dependent, however, there are a few performance tests on jsperf.com on this matter. It also comes down to the size of your data. Generally it is faster to use object key value pairs when you have large amounts of data. For small datasets, arrays can be faster.

这个问题的答案取决于浏览器,但是,jsperf.com 上有一些关于这个问题的性能测试。这也归结为数据的大小。通常,当您有大量数据时,使用对象键值对会更快。对于小数据集,数组可以更快。

Array search will have different performance dependent on where in the array your target item exist. Object search will have a more consistent search performance as keys doesn't have a specific order.

数组搜索将具有不同的性能,具体取决于目标项目在数组中的位置。对象搜索将具有更一致的搜索性能,因为键没有特定的顺序。

Also looping through arrays are faster than looping through keys, so if you plan on doing operations on all items, it can be wise to put them in an array. In some of my project I do both, since I need to do bulk operations and fast lookup from identifiers.

循环数组也比循环键快,所以如果你打算对所有项目进行操作,把它们放在一个数组中是明智的。在我的一些项目中,我两者都做,因为我需要进行批量操作和从标识符中快速查找。

A test:

一个测试:

http://jsben.ch/#/Y9jDP

http://jsben.ch/#/Y9jDP

回答by Beri

This problem touches all programming languages. It depends on many factors:

这个问题涉及所有编程语言。这取决于很多因素:

  • size of your collection -arrays will get slower when you are searching for the last key, and array is quite long
  • can elements repeat them selves-if yes, than you need a array. If no: you need either a dictionary (map) or you need to write a add method that for each add will iterate your array and find possible duplicates-that can be troublesome, when dealing with large lists
  • average key usage - you will lose performance, if the most requested userId is at the end of the list.
  • 当您搜索最后一个键时,集合的大小 -arrays 会变慢,并且数组很长
  • 元素是否可以自我重复 - 如果是,那么您需要一个数组。如果不是:您需要一个字典(地图)或者您需要编写一个 add 方法,该方法为每个添加将迭代您的数组并找到可能的重复项 - 在处理大型列表时可能会很麻烦
  • 平均密钥使用 - 如果请求最多的 userId 位于列表末尾,您将失去性能。

In your example map would be a better solution. Secondly, you need to add a break to yor code:)

在您的示例地图中,将是一个更好的解决方案。其次,您需要在您的代码中添加一个中断:)

var user;

for (var i = 0; i < users.length; i ++) {
  if (users[i].id == id) {
     user = users[i]; break;
  }
}

Or you will lose performance:)

否则你会失去性能:)

回答by Hendrik Janson

associative arrays are much slower then arrays with numbered indexes, because associative arrays work by doing string comparisons, which are much, much slower then number comparisons!

关联数组比带有编号索引的数组慢得多,因为关联数组通过进行字符串比较来工作,这比数字比较慢得多!