Sparse array and dense array in javascript
When learning about the apis related to the underscore. js array, I encountered sparse array, which I had never touched before.
Here we will learn what sparse arrays and dense arrays are.
What is dense array? In java and C, arrays are a continuous storage space with a fixed length. The actual location of the array is address and the length is n. The occupied storage space is address [0], address [1], address [2] ...... address [n-1]. That is to say, array elements are closely connected and there is no gap between them. The following js Code creates an intensive array.
var data = [3,1,6,9,2];
What is a sparse array? In contrast to dense arrays, javascript does not force array elements to be closely connected, that is, clearance is allowed. The following JavaScript code is valid:
Var sparse = new Array (); sparse [0] = 0; sparse [3] = 3; alert (sparse [0]); // output 0 alert (sparse [1]); // output undefined
1. Create a sparse array
The following code creates a sparse array with a fixed length.
var a = new Array(3); a[2] = 1; alert(a[0]);//undefined alert(a[2]);//1
To put it bluntly, it is easy to create a sparse array in js, as long as you intentionally leave gaps between array elements. For example
var arr = []; arr[0] = 0;arr[200] = 200;
2. Create a dense array
We can see that arrays in js are generally sparse, and it is generally troublesome to traverse sparse arrays.
var dense = Array.apply(null, Array(3));
This line of code is equivalent to var dense = Array (undefined, undefined, undefined); isn't it strange that this method is no different from sparse Array. Check the Code:
// Sparse array var Array = new array (3); array [2] = "name"; for (var a in array) {console. log ("index =" + a + ", value =" + array [a]);} // dense Array var dense = array. apply (null, Array (3); dense [2] = "name"; for (var a in dense) {console. log ("index =" + a + ", value =" + dense [a]);}
Observe the console with F12 and the output result is:
Examples + examples + PGJyPgo8L3A + CjxwPjxzdHJvbmc + examples + myorK7z/HO0sPH1NpDu/samples/1bzkoaM8L3A + CjxwPmphdmFzY3JpcHTW0Mr91 + examples DfUtq + 53MDt0rvQqQ = "Number" and length attributes.
More directly, arrays in JavaScript do not have indexes at all, because indexes should be numbers, while arrays in JavaScript are actually strings.
Arr [1] is actually an arr ["1"]. To arr ["1000"] = 1, arr. length is automatically changed to 1001.
The root cause is that the objects in JavaScript are key-value pairs from strings to any value.
Although sparse arrays and dense arrays are not much different, javascript does not have syntax to force arrays to be sparse or dense, which is just a conceptual distinction.
Best Practice: Consider js arrays as arrays in java or C. Our programmers are responsible for making js array elements continuous..
For example, var array = [1, 2, 4];
For example, var array = new Array (); array [0] = 0; array [1] = 1;
The js array created in this way conforms to the array we are familiar.