Sparse and dense arrays javascript arrays

Dense array

An array is a contiguous memory space, has a fixed length. In fact, the position is added to the array address, length is n, then the storage space is address [0], address [1], address [2] ....... address [n-1]. I.e., between the array elements are closely linked, there are no voids. The following is js code creates a dense array

1
var data = [3,1,6,9,2];

Sparse array

In contrast to the dense array, javascript not mandatory array elements are closely linked, allowing the presence of the gap. The following js code is legal:

 
1
2
3
4
5
var sparse = new Array();
sparse[0] = 0;
sparse[3] = 3;
alert(sparse[0]); //输出0
alert(sparse[1]); //输出undefined

1, create a sparse array

The following code creates a sparse array of fixed-length

1
2
3
4
var a = new Array(3);
a[2] = 1;
alert(a[0]); //undefined
alert(a[2]); //1

js established sparse arrays is easy, as long as there is a gap you deliberately between array elements. Such as

1
2
3
var arr = [];
arr[0] = 0;
arr[200] = 200;

2, create a dense array

Js array can be seen in the generally sparse, traversing generally sparse array is too much trouble.

1
var dense = Array.apply( null , Array(3));

This line is equivalent to var dense = Array (undefined, undefined, undefined); in this way there is no difference with the sparse array. Look at the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//稀疏数组
var array = new Array(3);
array[2] = "name" ;
for ( var a in array)
{
   console.log( "index=" + a + ",value=" + array[a]);
}
// 密集数组
var dense = Array.apply( null , Array(3));
dense[2] = "name" ;
for ( var a in dense)
{
   console.log( "index=" + a + ",value=" + dense[a]);
}

The output console is observed with F12:

Can see that indeed there is a difference: only traverse a sparse array (because only one element), a dense array traversal three times.

3, summary

Array in JavaScript is not as regular arrays to us in the language such as C or java, js array is not in a contiguous address space starting address + length thereof.

Array is actually a javascript objects, but automatically manages some "number" properties and the length property Bale.

He said more directly, the array in JavaScript there is no index, because the index should be figures, and the index of the array in JavaScript is actually a string.

arr [1] is actually arr [ "1"], to arr [ "1000"] = 1, arr.length will automatically change to 1001.

The root causes of these manifestations is this: The JavaScript object is the key to any string value pairs.

Although sparse array and dense array is not very different, javascript does not enforce the array syntax is sparse or dense, it is merely conceptual distinction.

Best practice is: put js array as an array of java or C, by our programmers to make responsible js array element is continuous.

Such as

1
var array = [1,2,3,4];

Such as:

1
var array = new Array();array[0]=0;array[1]=1;

js array created in this way, it is in line with the familiar array.

Guess you like

Origin www.cnblogs.com/zhuochong/p/11640098.html
Recommended