1

I was wondering if any one knows how memory is handled with JS arrays if you have an array that starts with a high value.

For example, if you have:

array[5000] = 1; 

As the first value in the array, everything before 5000 simply does not exist, will the amount of memory assigned to the array cater for the unassigned 4999 positions prior to it... or will it only assign memory to the value in the array for [5000] ?

I'm trying to cut down on the amount of memory used for my script so this led to me wondering about this question :)

Sir
  • 8,135
  • 17
  • 83
  • 146

3 Answers3

2

When assigning a value to the 5000th key, not the whole array is populated:

var array = [];     // Create array
array[5000] = 1;
'1' in array;       // false: The key does not exists
Object.keys(array); // 5000 (it's the only key)

If you want to blow your new browser with arrays, populate a typed array:

var array = new ArrayBuffer(6e9); // 6 Gigs

Both can be verified easily in Chrome: Open the console and memory console (Shift+Esc), and paste the code. window.a=new Array(6e9); or window.a=[];window[6e9]=1; doesn't result in a significant memory increase,
while window.a=new ArrayBuffer(6e9); crashes the page.

PS. 6e9 === 6000000000

Rob W
  • 341,306
  • 83
  • 791
  • 678
  • @Rob W so you're saying it will assign memory for all 5000 ? – Sir Apr 07 '12 at 15:54
  • @Dave Only memory for the 5000th key is reserved, the other keys do not exist, and no memory is reserved for these. – Rob W Apr 07 '12 at 15:57
  • @RobW so essentially - the arrays can be used like data dictionaries without an issue with memory...? – Sir Apr 07 '12 at 15:58
  • @Dave Yes, but it would be wiser to use object literals instead of an array, because you probably do not need these shiny array methods (`var x = {};x[5000]=1;` versus `var x=[];x[5000]=1;`). Another note: Keys are always strings. `x[5000]` is equivalent to `x['5000']`. – Rob W Apr 07 '12 at 16:02
  • Yeh i hate that it uses strings, forever using parse int >.> – Sir Apr 07 '12 at 16:07
  • 1
    @Dave Since you know that array keys are always integers, prefixing an unary `+` is sufficient: `+'1' === 1` . There are no side effects, see also: [Comparison between all number-conversion methods](http://stackoverflow.com/a/8112802/938089?are-there-are-any-side-effects-of-using-this-method-to-convert-a-string-to-an-in). – Rob W Apr 07 '12 at 16:08
  • @RobW so in an ideal world for this: http://www.paste.to/Mzc4ODEz i should not make an array but rather, objects? – Sir Apr 07 '12 at 16:12
  • @Dave What is your question regarding that script? (what is `sdata`?) – Rob W Apr 07 '12 at 16:15
  • @RobW can we move this to chat ? – Sir Apr 07 '12 at 16:19
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/9817/discussion-between-rob-w-and-dave) – Rob W Apr 07 '12 at 16:20
0

Javascript is really interpreted and run by the browser, so it depends on how the browser implements this behavior. In theory, once you do array[5000], you have an array of 5001 elements, all except the 5001st being undefined.

Though if I were the one implementing the logic for running such script, undefined would be the default value if not assigned to anything else, meaning I could probably get away with defining a map with 1 entry assigning key 5000 to value 1. Any accesses to any other value in the array would automatically return undefined, without having to do unnecessary work.

Here's a test of this here. As you can see, the alert is seen immediately.

Neil
  • 5,762
  • 24
  • 36
0

JS arrays are actually not arrays as you know them from other programming languages like C, C++, etc. They are instead objects with a array like way of accessing them. This means that when you define array[5000] = 1; You actually define the 5000 property of the array object.

If you had used a string as the array key you would have been able to access the index as a property as well to demonstrate this behavior, but since variable names can't start with a number array.5000 would be invalid.

array['key'] = 1;
alert( array.key ); // Gives you 1

This means that arrays will probably be implemented much like objects, although each implementation is free to optimize, thus giving you the behavior you except from objects where you can define object.a and object.z without defining the whole alphabet.

Andreas Hagen
  • 2,335
  • 2
  • 19
  • 34