• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

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

aceO07

Diamond Member
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.
 
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
 
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.
 
Wait... in Javascript an "array" can be either an actual array or a hashtable? No wonder people are confused...
 
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.
 
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.
 
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."
 
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 ;-) )
 
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?
 
"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).
 
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"]++? 😉
 
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?

🙂
 
Back
Top