Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory management of JavaScript Array [duplicate]

Possible Duplicate:
Are Javascript arrays sparse?

I am learning JavaScript at the moment and have been reading some simple introductions and tutorials. While looking at the Array object I stumbled upon some details, which strike me as very odd, coming from other languages like C/Java/Scala/...


So lets assume we define an array as such:

var arr = ['foo','bar','qux']

We now assign

arr[5] = 'baz'

which results in our array looking like this:

arr
>> ["foo", "bar", "qux", undefined, undefined, "baz"]

And the length is as expected

arr.length
>> 6

JavaScript has kindly expanded our array to the needed length - six - and the new items are set to undefined - except for the one we actually assigned a value to.

From a low level point of view this is horrible memory-wise. Typically an array would be a continuous range in memory - making an array bigger generally involves copying the whole array to a new memory location, sufficient in size. This is a very costly operation.

Now, I do realize that this is likely not what JavaScript engines are doing, as copying around arrays would be crazy expensive and the memory space would be wasted on all these 'undefined' values.

Can someone tell me what actually happens behind the door?

  • Are arrays actually some sort linked lists?
  • Are the 'undefined' array items actually there?
  • How expensive is it to work with large arrays that are mostly filled with 'undefined'?
like image 620
fgysin Avatar asked Sep 28 '12 12:09

fgysin


People also ask

How are arrays stored in memory JavaScript?

In Javascript, an array is a Hashtable Object type so the interpreter doesn't need to keep track of physical memory and changing the value of an element doesn't affect other elements as they're not stored in a contiguous block of memory.

How do I free up memory in JavaScript?

To release memory, assign the global variable to null . window. users = null; I want to make this article as easy to understand as possible.


2 Answers

In the first version of JavaScript, there were no arrays. They were later introduced as a sub-class of that "mother of all objects": Object. You can test this quite easily by doing this:

var foo = [1,2,3,4];
for (var n in foo)
{//check if n is equal (value and type) to itself, coerced to a number
    console.log(n === +(n) ? 'Number' : 'String');
}

This will log String, time and time again. Internally, all numeric keys are converted to strings. The Length property merely fetches the highest index, and adds 1 to it. Nothing more. When you display your array, the object is iterated, and for each key, the same rules apply as for any object: first the instance is scanned, then the prototype(s)... so if we alter our code a bit:

var foo = [1,2,3,4];
foo[9] = 5;
for (var n in foo)
{
    if (foo.hasOwnProperty(n))
    {//check if current key is an array property
        console.log(n === +(n) ? 'Number' : 'String');
    }
}

You'll notice the array only has 5 own properties, the undefined keys 4-8 are undefined, because there was no corresponding value found within the instance, nor in any of the underlying prototypes. In short: Arrays aren't really arrays, but objects that behave similarly.

As Tim remarked, you can have an array instance with an undefined property that does exist within that object:

var foo = [1,2,undefined,3];
console.log(foo[2] === undefined);//true
console.log(foo[99] === undefined);//true

But again, there is a difference:

console.log((foo.hasOwnProperty('2') && foo[2] === undefined));//true
console.log((foo.hasOwnProperty('99') && foo[99] === undefined));//false

RECAP, your three main questions:

  • Arrays are objects, that allow you to reference their properties with numeric instances

  • The undefined values are not there, they're merely the default return value when JS scans an object and the prototypes and can't find what you're looking for: "Sorry, what you ask me is undefined in my book." is what it says.

  • Working with largely undefined arrays doesn't affect the size of the object itself, but accessing an undefined key might be very, very marginally slower, because the prototypes have to be scanned, too.

Update:

Just quoting the Ecma std:

15.4 Array Objects
Array objects give special treatment to a certain class of property names. A property name P (in the form of a String value) is an array index if and only if ToString(ToUint32(P)) is equal to P and ToUint32(P) is not equal to 2^32 1. A property whose property name is an array index is also called an element. Every Array object has a length property whose value is always a nonnegative integer less than 2^32. The value of the length property is numerically greater than the name of every property whose name is an array index; whenever a property of an Array object is created or changed, other properties are adjusted as necessary to maintain this invariant. Specifically, whenever a property is added whose name is an array index, the length property is changed, if necessary, to be one more than the numeric value of that array index; and whenever the length property is changed, every property whose name is an array index whose value is not smaller than the new length is automatically deleted. This constraint applies only to own properties of an Array object and is unaffected by length or array index properties that may be inherited from its prototypes.

An object, O, is said to be sparse if the following algorithm returns true:
1. Let len be the result of calling the [[Get]] internal method of O with argument "length".
2. For each integer i in the range 0≤i
    a. Let elem be the result of calling the [[GetOwnProperty]] internal method of O with argument        ToString(i).
     b. If elem is undefined, return true.
3. Return false.

like image 150
Elias Van Ootegem Avatar answered Sep 19 '22 12:09

Elias Van Ootegem


Arrays are just an ordered list of objects. In JavaScript everything is an object, so arrays are not really arrays as we know them :)

You can find little internals here.

For your doubts about working with large arrays... Well, remember that the less calculation you make "client-side", the faster will be your page.

like image 27
napolux Avatar answered Sep 20 '22 12:09

napolux