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: which says array lookup is 20 % faster.
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./sparse-objects/3 which says array lookup is 20 % faster.
Share Improve this question edited Jan 16, 2013 at 12:50 Rune Jeppesen asked Jan 16, 2013 at 10:02 Rune JeppesenRune Jeppesen 1,13113 silver badges24 bronze badges 13- 3 Why do you care about memory? Choose the best data structure for your use case. – Florian Margaine Commented Jan 16, 2013 at 10:03
- @BenM it's not a duplicate. The other question is about speed, this one is about memory (and speed). – Florian Margaine Commented Jan 16, 2013 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 Commented Jan 16, 2013 at 10:06
- BenM pointed out this very related question - thank you @FlorianMargaine I would like to understand javascript better and because there will be thousands of items. – Rune Jeppesen Commented Jan 16, 2013 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 Commented Jan 16, 2013 at 10:21
1 Answer
Reset to default 7I 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/ryanwheale/HbHxv`
And here is another with a little more output and control:
// see code in fiddle
http://jsfiddle/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.