I have been accumulating a series of data that increases gradually in size. Now, I am faced with the task of finding a specific row within this data using its unique Id. In my quest to optimize this search process, I have considered two potential options. The first involves creating an array and continuously pushing new rows into it. When the time comes to find a particular row, I would then sift through the items in the array or utilize the array prototype function (find). On the other hand, the second option proposes creating an object where each new row is added as a property, with the name of the property being the Id of the respective row. Then, whenever I need to retrieve a row, I simply access the property of the object by its name (Id). Now, the burning question remaining is: which of these options represents the most efficient approach? Or perhaps there exists a third alternative I have yet to consider?
Option One:
const array = [
{
"Id":15659,
"FeederCode":169,
"NmberOfRepetition":1
},
{
"Id":15627,
"FeederCode":98,
"NmberOfRepetition":2
},
{
"Id":15557,
"FeederCode":98,
"NmberOfRepetition":1
}
]
For each new row, it gets pushed into this array. To access:
array.find(x => x.Id === 15659)
Option Two:
const object = {
15659:{
"Id":15659,
"FeederCode":169,
"NmberOfRepetition":1
},
15627:{
"Id":15627,
"FeederCode":98,
"NmberOfRepetition":2
},
15557:{
"Id":15557,
"FeederCode":98,
"NmberOfRepetition":1
}
}
When a new row arrives, a new property is added to this object.
To access: object[15659]
Edit: I once came across information suggesting that adding new properties to existing objects can be quite costly.