2

What is the more efficient (in terms of memory consumed) way to store an array of length 10,000 with 4 integer properties:

Option 1:Array of objects

var array = [];
array[0] = {p1:1, p2:1, p3:1, p4:1}

or

Option 2: Four arrays of integers

var p1 = [], p2 = [], p3 = [], p4 = [];
p1[0] = 1;
p2[0] = 1;
p3[0] = 1;
p4[0] = 1;
6
  • 4
    There are two valid answers here: "Try it yourself and find out", and "It doesn't matter". It is trivially easy for you to write a benchmark script and figure out which one consumes more memory. You shouldn't care though, because the difference will be negligible and memory consumption at this scale is not important in developing a website. Whichever option leads to more logically sound, maintainable code is the one you should use. Commented Jun 22, 2012 at 18:13
  • @meagar there is also "it depends on the javascript engine implementation" Commented Jun 22, 2012 at 18:15
  • besides, all answer bellow forget that depending on how you create it a javascript array can take a lot more that the size it needs to store it's elements. see for a discussion about very similar problems stackoverflow.com/questions/5040753/… Commented Jun 22, 2012 at 18:18
  • 1
    @meagar, My PC is way more powerful that entire army-grade SDI systems of old. Every time it hickups on launching Notepad, I want to find somebody who said "it doesn't matter" while developing this software and shake their neck. Commented Jun 22, 2012 at 18:19
  • @Oleg What about when your software crashes because some hackish developer spent all his time on irrelevant micro-optimiztaions and produced an unmaintainable, illegible pile of spaghetti? Commented Jun 22, 2012 at 18:21

4 Answers 4

1

Option 2. 4 objects (arrays are objects too) vs. 10001 objects.

Sign up to request clarification or add additional context in comments.

Comments

0

Four arrays of 10,000 elements is probably better in terms of memory, because you're only storing four complex objects (arrays) and then 40,000 integers -- where the other way you are storing 10,000 arrays and 40,000 integers (4 per array).

Comments

0

My guess would be that, purely from a bits and bytes standpoint, a single multi-dimensional array would have the smallest footprint:

var p = [];
p[0] = [1,1,1,1];

2 Comments

It isn't single array - two dimensions are "emulated" by arrays inside array. It is 2 arrays - one outer and one inside. With 10000 elements it will become 10001 arrays.
True. Four 10,000 item arrays vs 10,000 four item arrays...the former probably wins
0

I actually tested both options with the Google Chrome task manager, which shows information about the open tabs (Shift+ESC), and while this not might be 100% accurate, it does show significant differences:

For the first option, creating an array with 10,000 elements, each being an object with 4 properties as you specified, the memory usage jumped by about 10MB after initiating the array.

The second option, creating 4 arrays with 10,000 elements each, made the memory usage jump by about 5MB.

Some of that memory usage jump might be related to the actual processing of the creation and internal browser stuff, but the point is - as expected - creating objects is adding more overhead for the data you are storing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.