16

I have a very basic programing question that i hoped you can shed a light on.

I am working a lot of objects right now and i was wondering if it is better to search for content inside an array of objects or inside a nested object?

For eg, i can store the same data sample in the following two ways:

data1 = [ 
{ "id":1, "key1: "value1", "key2:"value2"},
{ "id":2, "key1: "value1", "key2:"value2"},
{ "id":3, "key1: "value1", "key2:"value2"},
{ "id":4, "key1: "value1", "key2:"value2"},
.....
]

and

data2 = {
"id_1": { "key1: "value1", "key2:"value2"},
"id_2": { "key1: "value1", "key2:"value2"},
"id_3": { "key1: "value1", "key2:"value2"},
"id_4": { "key1: "value1", "key2:"value2"},
.....
}

Now the requirement is to get a certain property from a child object. And all we know is the id (and not the index) associated with it.

If i were to use the array method, i will have to use loops and array filters to access any content/value in the individual objects. This method seems rather cumbersome to be and iterating through each child object feels very inefficient to me. Yet whenever i see similar data samples being implemented by experienced programmers, they all seem to using arrays a lot.

If I were to use the nested object method, all i have to do is call data2.id_2.key2 to get that specific value.

Which is the recommended way of doing things? I will be playing with rather large datasets and so, option will have better performance?

Rishav Sharan
  • 2,763
  • 8
  • 39
  • 55
  • 2
    Of course access by property key is much much faster. But it's easy to benchmark your code, you should do it. – Denys Séguret Jul 22 '14 at 08:58
  • 2
    If you need to sort theses data, you should use an array. Otherwise, use the object way – Gwenc37 Jul 22 '14 at 08:59
  • If you need to access individual items and you have the key without needing to search through the structure for it, then use the hash. Structures this small, any performance difference is liable to be trivial. – Tony Hopkinson Jul 22 '14 at 09:01
  • `data2.id2.key2` would failed with the object you posted btw ;) And to be fair to Array you could do `data1[1].key2`. – GillesC Jul 22 '14 at 09:02
  • Thanks. i understand that for small datasets, its not a question worth exploring. But i will be using heavy graphics objects as the child objects and was wondering which way will be faster for me to access the inner content. I suppose array sorting isnt really useful to me. – Rishav Sharan Jul 22 '14 at 09:06
  • 2
    And what about [using both at the same time](http://jsfiddle.net/p42Vj/2/)? It's a *real question*, i.e. I'm not telling this is the solution ;) – sp00m Jul 22 '14 at 09:28

2 Answers2

9

It's a kind of question with no wrong answers. Both cases are good enough. Depends on case. You should think about the semantics. Do you return array of elements (i.e. list of users, etc.) or just object with a lot of properties within? This structure should depend on data, I guess. But keep in mind, that access by property object.property is faster than array[index].

Dmitry Volokh
  • 1,630
  • 16
  • 28
  • Thanks. It seems that i should use object.property in that case. I will not mark your answer as the final one right now just in case someone else wants to weigh in on this. – Rishav Sharan Jul 22 '14 at 09:08
  • 1
    Also I should notice that for about 90% cases doesn't matter which structure would be used (from performance point of view) because there a lot of other places in any app, where perfomance could be increased much effectively that just changing acces type to properties. – Dmitry Volokh Jul 22 '14 at 09:12
  • Interesting, I'd have guessed they were both equally fast. Got a source to back that up? – Emil Lundberg Jul 22 '14 at 09:14
  • @EmilLundberg Your just could do a small test on jsperf.com and check this. – Dmitry Volokh Jul 22 '14 at 09:31
3

Understanding Objects vs Arrays and When to Use Them

  • What are Objects and how do they differ from Arrays in Javascript?
  • When is it advantageous to use one over the other?

Arrays: Arrays come with several, very useful native methods. We can add a new element to existing array instance using push() and remove the last element from the array via pop(). We can also use splice() to remove n elements and/or insert new element(s) at index i.

Object: Think about objects as associative arrays, i.e. list of key -> value pairs. These keys are referred to as object properties.

Checking if Property or Value Exists

Arrays: Generally when we work with arrays, we care less about indexes and more about values. One of the common operations we perform with Arrays is checking if a certain value is in the array. This is easily accomplished using indexOf() method.

Object: In contrast to Arrays, we generally want to know if an Object contains a certain property. Usually we will write a function that takes Object as an argument and will expect that it contains a certain set of properties. This Object can come from an API or some other piece of code and we shouldn't rely on it having all the properties we expect. It is always a good idea to check whether the property exists before accessing the value behind that property. Objects come with hasOwnProperty() method which allows us to do just that.

extracted from here

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231