Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is a dense array?

The explanation for a dense array that I read from a few pages seem to be in contradiction to one another. I'd like some help understanding what it is.

While some links (search result 1, search result 2) suggest that it simply is an array where:

  1. the elements of the array are known to be specific values; and
  2. are assigned to the array at the time of its initialization.

The allusion there is that JavaScript arrays are dense.

It all makes sense up until here.

But this statement taken from the JavaScript Guide on the Mozilla Developer Network (MDN) says:

Since an array's length can change at any time, and data can be stored at non-contiguous locations in the array, JavaScript arrays are not guaranteed to be dense; this depends on how the programmer chooses to use them. In general, these are convenient characteristics; but if these features are not desirable for your particular use, you might consider using typed arrays.

And this has confused me now. My question is:

What does the statement on the MDN page mean when it says JavaScript arrays are not guaranteed to be dense? If it means that the following is not a dense array because one or more of its elements are undefined at the time of initialization, then why do the links I listed above seem to indicate that JavaScript arrays are indeed dense?

var array = new Array(1, , 3, ); // [1, undefined, 3, undefined]
like image 384
Water Cooler v2 Avatar asked Aug 19 '16 02:08

Water Cooler v2


People also ask

What is sparse and dense array?

A sparse array is an array of data in which many elements have a value of zero. This is in contrast to a dense array, where most of the elements have non-zero values or are “full” of numbers. A sparse array may be treated differently than a dense array in digital data handling.

What is sparse array in C++?

A sparse matrix is a matrix in which majority of the elements are 0. An example for this is given as follows. The matrix given below contains 5 zeroes. Since the number of zeroes is more than half the elements of the matrix, it is a sparse matrix.

What is sparse set?

A sparse set is a specialized data structure for representing a set of integers. It can be useful in some very narrow and specific cases, namely when the universe of possible values is very large but used very sparingly and the set is iterated often or cleared often.


2 Answers

"Dense" is in opposition to "sparse", and generally is used when talking about storage. For example, this array is dense:

a = [undefined, undefined, 2]

It can be stored in memory exactly like that: a sequence of three locations, the first two being undefined, the third being 2.

This array is sparse:

a = []
a[100000000] = 100000000

It is not stored in memory as a sequence of 100000001 locations, as it would be horribly inefficient. It is definitely not 100000000 places of undefined followed by 100000000. Rather, it just says 100000000th one is 100000000, and there is no space allocated to the first 100000000 elements.

(Actually, try to do this with 2 instead of 100000000, and you'll notice a curious thing: Chrome will display the dense array as [undefined, undefined, 2], but the sparse one as [undefined × 2, 2].)

like image 176
Amadan Avatar answered Sep 19 '22 19:09

Amadan


Those articles say you can create an array being dense. This means that, at the time of creation, such arrays are dense, since in arrays like:

var a = new Array("foo", "bar", "baz");
var b = [2, 3, 5];

every element is set: from 0 to length-1, there is no undefined value. Or better said: every position from 0 to length-1 was assigned a value (even if the value is, actually, undefined).

However, you can make those arrays not dense anymore, by doing something like this:

a[20] = "bat";

That array, which was dense, is not dense anymore since the elements 0 1 2 and 20 (unlike elements in 3 to 19) are set to a value (this array has 4 elements, not 21).

like image 21
Luis Masuelli Avatar answered Sep 22 '22 19:09

Luis Masuelli