Javascript: Does it waste memory if I have a big array that is mostly empty.

aceO07

Diamond Member
Nov 6, 2000
4,491
0
76
I'm using javascript array to hold objects and in order to identify these objects I'm using their database id as their index. So this means there might be an array that has objects in index 1110 and 4520, but the other array elements are all empty.

Does this cause problems or waste memory? I might also consider using Prototype Hash.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,836
4,815
75
It appears that the array is made as a hash, so memory is not wasted. This scriptlet proves it to me:

javascript:var myArray = new Array();myArray[11411411] = 1;myArray[27222722] = 2;for (var i in myArray) {document.writeln('key is: ' + i + ', value is: ' + myArray[ i ]);}

Since it prints only the two values, and doesn't take forever, it has to be a hash.

Edit: array index
 

blahblah99

Platinum Member
Oct 10, 2000
2,689
0
0
Originally posted by: aceO07
I'm using javascript array to hold objects and in order to identify these objects I'm using their database id as their index. So this means there might be an array that has objects in index 1110 and 4520, but the other array elements are all empty.

Does this cause problems or waste memory? I might also consider using Prototype Hash.

You dont' need to create an array with 3000 elements... just use their id as the hash key. Javascript allows this.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Wait... in Javascript an "array" can be either an actual array or a hashtable? No wonder people are confused...
 

ahurtt

Diamond Member
Feb 1, 2001
4,283
0
0
Originally posted by: Ken g6
It appears that the array is made as a hash, so memory is not wasted. This scriptlet proves it to me:

javascript:var myArray = new Array();myArray[11411411] = 1;myArray[27222722] = 2;for (var i in myArray) {document.writeln('key is: ' + i + ', value is: ' + myArray[ i ]);}

Since it prints only the two values, and doesn't take forever, it has to be a hash.

Edit: array index

Out of curiosity, try the same thing but instead of using the "for. . in" construct for your loop, use a traditional old for loop like:

for(var i = 0; i < myArray.length; ++i) {//code. . .}

What do you get then? I suspect the reason your code executed so quickly might be because a key set containing only 2 entries is implicitly created and iterated over when you use the for. . .in construct effectively ignoring all the empty indexes. That doesn't necessarily mean the Array object is internally implemented as a hash. Maybe it is but maybe not. I really don't know. I just thought it was an interesting philosophical question.
 

Cogman

Lifer
Sep 19, 2000
10,286
147
106
Doesn't a hash table by its very nature have a lot of empty spaces? The more full a hash table is, the slower it goes.

So, if you aren't doing a lot of sequential work (IE, first I do this object, then the next object, ect) but rather something more random (Object B requests some data from Object 320). Then a hash table is the way to go. However, if that's not the case then a regular array would be better to use.
 

esun

Platinum Member
Nov 12, 2001
2,214
0
0
From a Google search:

"JavaScript arrays are sparse arrays ? JavaScript allows arrays to be sparse therefore, when you write arrayObj = new Array(100); unlike C runtime, JScript doesn't allocate any slot/memory for those 100 entries up front. It allocates slot for an index only when an index is to be populated with a value."
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: esun
From a Google search:

"JavaScript arrays are sparse arrays ? JavaScript allows arrays to be sparse therefore, when you write arrayObj = new Array(100); unlike C runtime, JScript doesn't allocate any slot/memory for those 100 entries up front. It allocates slot for an index only when an index is to be populated with a value."

Cute. I learned something today (never trust Javascript to do what you ask ;-) )
 

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
So, if I write:

var X = new Array(100);
X["Bob"] = "BOB";

which of those 100 cells is "BOB" actually in?


Really, whats the point of statically sizing an array at all, under those circumstances?
 

esun

Platinum Member
Nov 12, 2001
2,214
0
0
"BOB" isn't in any of those 100 cells (which haven't actually been allocated in memory). It's in a cell labeled "Bob", which you could consider a 101st cell (and cells 0-99 still exist, but again, are not allocated).
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: esun
"BOB" isn't in any of those 100 cells (which haven't actually been allocated in memory). It's in a cell labeled "Bob", which you could consider a 101st cell (and cells 0-99 still exist, but again, are not allocated).

...which begs the question, what about X["Bob"]++? ;)
 

DannyBoy

Diamond Member
Nov 27, 2002
8,820
2
81
www.danj.me
Originally posted by: esun
From a Google search:

"JavaScript arrays are sparse arrays ? JavaScript allows arrays to be sparse therefore, when you write arrayObj = new Array(100); unlike C runtime, JScript doesn't allocate any slot/memory for those 100 entries up front. It allocates slot for an index only when an index is to be populated with a value."

I love how many times this catches people out

arrayObj.length == 100

WTF?

:)