简体   繁体   中英

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. 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). and when I want to find a row just get the property of this object by its name(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)

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]

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 .

Complexity of search in Object is O(1) and in Array is O(n) . Hence, to yield better performance, you should use 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. 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. But recently I was reading about V8 optimizations and wanted to check, so used benchmark js for confirmation.

Here are my findings -

Number of entries in obj or arr : 100000

  1. Number of fetch operations from Obj: 47,174,859 ops/sec
  2. Number of search operation from Array: 612 ops/sec

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

  1. Number of fetch operations from Obj: 44,264,116 ops/sec
  2. Number of search operation from Array: 520,709 ops/sec

Number of entries in obj or arr : 10

  1. Number of fetch operations from Obj: 46,739,607 ops/sec
  2. Number of search operation from Array: 3,611,517 ops/sec

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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