9

What will use more memory, items1 where each item is an array or items2 where each item is an object:

var items1=[['James Bond',8,40],
...,
['Superman',9999,36]];
var items2=[{Name,'James Bond',strength:8,coolness:40},
...,
{Name,'Superman',strength:9999,coolness:36}];

Which will be the fastes way to fetch data search1 or search2?

var search1=items[432][2];
var search2=items2[432]["coolness"];

PS: The given scores are unofficial and my personal opinion of the 2 characters

SECOND EDIT: I had a picture of a test but it was scewed as pointed out by Felix. This is more correct: http://jsperf.com/sparse-objects/3 which says array lookup is 20 % faster.

Rune Jeppesen
  • 1,121
  • 13
  • 22
  • 3
    Why do you care about memory? Choose the best data structure for your use case. – Florian Margaine Jan 16 '13 at 10:03
  • @BenM it's not a duplicate. The other question is about speed, this one is about memory (and speed). – Florian Margaine Jan 16 '13 at 10:05
  • I imagine arrays are cheaper, less memory intensive - but I would trade a little bit of memory for readability, debugability, maintainability, and extensibility. – Ryan Wheale Jan 16 '13 at 10:06
  • [BenM pointed out this very related question - thank you](http://stackoverflow.com/questions/8423493/what-is-the-performance-of-objects-arrays-in-javascript-specifically-for-googl) @FlorianMargaine I would like to understand javascript better and because there will be thousands of items. – Rune Jeppesen Jan 16 '13 at 10:20
  • Browsers like Chrome have profilers with which you can make these tests. But I'd say arrays need less memory since the property names are shorter (e.g. `'0'` vs `'Name'`). In the end it's always a tradeoff between memory and speed and you have to choose the data structure that solves your problem and fits your requirements. – Felix Kling Jan 16 '13 at 10:21
  • @Ryan Wheale it will be used in a plugin where it will be a parameter so readability, debugability, maintainability and extensibility do not matter so much – Rune Jeppesen Jan 16 '13 at 10:24
  • @FelixKling are you saying that search2 will be faster than search1? – Rune Jeppesen Jan 16 '13 at 10:26
  • @Rune: No, and it's unlikely. There might be implementations that optimize array access, but eventually arrays are just objects that handle numeric properties in a special way. – Felix Kling Jan 16 '13 at 11:01
  • @FelixKling I just edited the post - If I understand it correct search 2 is faster!? – Rune Jeppesen Jan 16 '13 at 11:05
  • The performance test is a bit skewed due to the string concatenation I presume. Have a look at this test: http://jsperf.com/sparse-objects. One reason why object access is potentially faster (setting is for sure) is that JS does not have to check whether the property name is a valid array index. – Felix Kling Jan 16 '13 at 11:32
  • @FelixKling excellent point! Strange it is a factor 30. I tried this one [link](http://jsperf.com/sparse-objects/3) where Test 3 and 4 are those most like the scenario in which I will use it. In that case array is 20% faster – Rune Jeppesen Jan 16 '13 at 12:25
  • I am not the best at writing unit tests, but here's a simple example that tells me that there's not too much of a difference: http://jsfiddle.net/ryanwheale/HbHxv/ – Ryan Wheale Jan 16 '13 at 18:17
  • @RyanWheale very strange how the tests show different things. I remember seeing a post about using date differences as test results is dangerous. But if you'll write is as an answer I'll accept it. I think I will go with array simply because the json transfer will then be without thousands of redundant field names – Rune Jeppesen Jan 17 '13 at 09:42

1 Answers1

6

I am not the best at writing unit tests, but here's a simple example that tells me that there's not too much of a difference:

// see code in fiddle

http://jsfiddle.net/ryanwheale/HbHxv`

And here is another with a little more output and control:

// see code in fiddle

http://jsfiddle.net/ryanwheale/HbHxv/4/

I like the 2nd option because you can see how much memory each method takes by using Chrome's Timeline in the dev tools. Start recording, and click the "objects" button several times, wait a few seconds and then click the "arrays" button several times. You will see the memory consumed by objects is more than the memory used by Arrays. There also seems to be a slight favor for arrays in terms of time of execution. However, we are talking about a million items... which is highly unrealistic.

Ryan Wheale
  • 26,022
  • 8
  • 76
  • 96