title: node.js arrays vs. objects from utilized memory point of view
body: <p>Wanted to share a simple experiment I ran, using node.js v6.11.0 under Win 10. </p>
  
  <p><strong>Goal</strong>. Compare arrays vs. objects in terms of memory occupied.</p>
  
  <p><strong>Code</strong>. Each function <code>reference</code>, <code>twoArrays</code>, <code>matrix</code> and <code>objects</code> create two arrays of same size, containing random numbers. They organize the data a bit differentely.</p>
  
  <p><code>reference</code> creates two arrays of fixed size and fills them with numbers.</p>
  
  <p><code>twoArrays</code> fills two arrays via push (so the interpreter doesn't know the final size).</p>
  
  <p><code>objects</code> creates one array via push, each element is an object containing two numbers.</p>
  
  <p><code>matrix</code> creates a two-row matrix, also using push.</p>
  
  <pre><code>const SIZE = 5000000;
  let s = [];
  let q = [];
  function rand () {return Math.floor(Math.random()*10)}
  function reference (size = SIZE) {
      s = new Array(size).fill(0).map(a =&gt; rand());
      q = new Array(size).fill(0).map(a =&gt; rand());
  }
  function twoArrays (size = SIZE) {
      s = [];
      q = [];
      let i = 0;
      while (i++ &lt; size) {
          s.push(rand());
          q.push(rand());
      }
  }
  function matrix (size = SIZE) {
      s = [];
      let i = 0;
      while (i++ &lt; size) s.push([rand(), rand()]);
  }
  function objects (size = SIZE) {
      s = [];
      let i = 0;
      while (i++ &lt; size) s.push({s: rand(), q: rand()});
  }
  </code></pre>
  
  <p><strong>Result</strong>. After running each function separately in a fresh environment, and after calling <code>global.gc()</code> few times, the Node.js environment was occupying the following memory sizes:</p>
  
  <p><code>reference</code>: 84 MB</p>
  
  <p><code>twoArrays</code>: 101 MB</p>
  
  <p><code>objects</code>: 249 MB</p>
  
  <p><code>matrix</code>: 365 MB</p>
  
  <p><code>theoretical</code>: assuming that each number takes 8 bytes, the size should be 5*10^6*2*8 ~ 80 MB</p>
  
  <p>We see, that <code>reference</code> resulted in a lightest memory structure, which is kind of obvious. </p>
  
  <p><code>twoArrays</code> is taking a bit more of memory. I think this is due to the fact that the arrays there are dynamic and the interpreter allocates memory in chunks, as soon as next push operation is exceeding preallocated space. Hence the final memory allocation is done for more than 5^10 numbers.</p>
  
  <p><code>objects</code> is interesting. Although each object is kind of fixed, it seems that the interpreter doesn't think so, and allocates much more space for each object then necessary. </p>
  
  <p><code>matrix</code> is also quite interesting - obviously, in case of explicit array definition in the code, the interpreter allocates more memory than required.</p>
  
  <p><strong>Conclusion</strong>. If your aim is a high-performance application, try to use arrays. They are also fast and have just O(1) time for random access. If the nature of your project requires objects, you can quite often simulate them with arrays as well (in case number of properties in each object is fixed).</p>
  
  <p>Hope this is usefull, would like to hear what people think or maybe there are links to some more thorough experiments...</p>