Can reducing index length in Javascript associative array save memory

Posted by d777 on Stack Overflow See other posts from Stack Overflow or by d777
Published on 2013-06-26T03:12:30Z Indexed on 2013/06/26 4:21 UTC
Read the original article Hit count: 93

Filed under:

I am trying to build a large Associative Array in JavaScript (22,000 elements). Do I need to worry about the length of the indices with regards to memory usage?

In other words, which of the following options saves memory? or are they the same in memory consumption?

Option 1:

  var student = new Array(); 
  for (i=0; i<22000; i++)
   student[i] = {
   "studentName": token[0],
   "studentMarks": token[1],
   "studentDOB": token[2]
   };

Option 2:

  var student = new Array(); 
  for (i=0; i<22000; i++)
   student[i] = {
   "n": token[0],
   "m": token[1],
   "d": token[2]
   };

I tried to test this on Google Chrome DevTools, but the numbers are inconsistent to make a decision. My best guess is that because the Array indices repeat, the browser can optimize memory usage by not repeating them for each student[i], but that is just a guess.

Thanks.

© Stack Overflow or respective owner

Related posts about JavaScript