简体   繁体   English

JavaScript中的对象访问与数组访问

[英]object access vs array access in javascript

I have a series of data and the size of it increases gradually. 我有一系列数据,并且它的大小逐渐增加。 I want to find a specific row of my data with its Id. 我想查找其ID的数据的特定行。 I have two options. 我有两个选择。 first: create an array and push every new row to this array and every time I want a row just search through items in the array or use array prototype function (find). 首先:创建一个数组,然后将每行添加到该数组中,每次我想要一行时,只需搜索数组中的项目或使用数组原型函数(查找)。 the other option is to create an object and every time a new row comes just add this row as a property (and the property name would be the Id of the row). 另一个选择是创建一个对象,每次出现新行时,只需将该行添加为属性(属性名称将为该行的ID)。 and when I want to find a row just get the property of this object by its name(Id). 当我想查找一行时,只需通过其名称(Id)获取该对象的属性即可。 Now I want to know which option is the most efficient way? 现在我想知道哪种选择是最有效的方法? (or is there a third option?) (或者还有第三种选择?)

first option: 第一种选择:

const array = [  
       {  
          "Id":15659,
          "FeederCode":169,
          "NmberOfRepetition":1
       },
       {  
          "Id":15627,
          "FeederCode":98,
          "NmberOfRepetition":2
       },
       {  
          "Id":15557,
          "FeederCode":98,
          "NmberOfRepetition":1
       }
    ]

each time a new row comes a new is pushed into this array. 每次出现新行时,都会将新行推入此数组。 access : array.find(x => x.Id === 15659) 访问: array.find(x => x.Id === 15659)

second option: 第二种选择:

const object =   {  
       15659:{  
          "Id":15659,
          "FeederCode":169,
          "NmberOfRepetition":1
       },
       15627:{  
          "Id":15627,
          "FeederCode":98,
          "NmberOfRepetition":2
       },
       15557:{  
          "Id":15557,
          "FeederCode":98,
          "NmberOfRepetition":1
       }
    }

each time a new row comes a new property is added to this object. 每次出现新行时,都会向该对象添加一个新属性。 access : object[15659] 访问: object[15659]

edit: I read somewhere that adding new properties to existing object has too much cost. 编辑:我读到某处,将新的属性添加到现有对象具有太多的成本。

In case you are looking forward to perform search operation then you should use Object as it gives better performance as compared to search in Array . 如果您希望执行搜索操作,则应使用Object ,因为与Array搜索相比,它提供了更好的性能。

Complexity of search in Object is O(1) and in Array is O(n) . Object搜索的复杂度为O(1)Array搜索的复杂度为O(n) Hence, to yield better performance, you should use Object . 因此,要获得更好的性能,应使用Object

Well in the first example you will have to iterate the array every time, when using Find. 好吧,在第一个示例中,每次使用“查找”时,您都必须迭代数组。

In the second example you will be accessing a property directly, leading to O(1) execution time, always fixed, no matter how many items are in there. 在第二个示例中,无论其中有多少项,您都将直接访问属性,这将导致O(1)执行时间始终固定。 So for better performance you ought to go by your 2nd way 因此,为了获得更好的性能,您应该选择第二条路

Reading from objects is faster and takes O(1) time, like @NikhilAggarwal Just said. 从对象读取速度更快,并且需要O(1)时间,就像@NikhilAggarwal刚说的那样。 But recently I was reading about V8 optimizations and wanted to check, so used benchmark js for confirmation. 但是最近我正在阅读有关V8优化的内容,并希望进行检查,因此使用了基准js进行确认。

Here are my findings - 这是我的发现-

Number of entries in obj or arr : 100000 obj或arr中的条目数:100000

  1. Number of fetch operations from Obj: 47,174,859 ops/sec 从Obj提取操作的次数: 47,174,859 ops / sec
  2. Number of search operation from Array: 612 ops/sec 数组搜索操作的次数: 612次操作/秒

If we reduce the entries - The number of operations for object almost remains the same but increases exponentially for arrays. 如果我们减少条目-对象的操作数几乎保持不变,但对数组则成倍增加。

Number of entries in obj or arr : 100 obj或arr中的条目数:100

  1. Number of fetch operations from Obj: 44,264,116 ops/sec 从Obj提取操作的次数: 44,264,116个操作/秒
  2. Number of search operation from Array: 520,709 ops/sec 数组搜索操作的次数: 520,709 ops / sec

Number of entries in obj or arr : 10 obj或arr中的条目数:10

  1. Number of fetch operations from Obj: 46,739,607 ops/sec 从Obj提取操作的次数: 46,739,607 ops / sec
  2. Number of search operation from Array: 3,611,517 ops/sec 数组搜索操作的次数: 3,611,517 ops / sec

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

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