Basic JavaScript Array knowledge
Preface:
In fact, as a Java programmer, I have always been a little "dismissive" about JS programs. Many people must be like me. If they have other language basics, JS syntax will not bother to read, writing a program directly is generally enough for work. However, recently I am working on a Web Excel project, which requires a lot of JS programming and optimization, and takes the time to "complement" The JS basics. I found that the original understanding of JS is still quite different. However, JS is actually easier than Java.
Basics:
The JS array is similar to the Java array. The values are ordered and the index starts from 0. However, the maximum possible index of the JS array is 2 32 times, which can accommodate up to 4294967295 elements. However, unlike Java arrays, JS arrays are dynamic and do not need to specify the initial size. They can be sparse and their subscripts are not necessarily continuous. If no element exists, undefined is returned. The most important thing is that JS arrays run through the concept that everything in JS is an object. JS Arrays can be treated as common objects, which is also the "essence" of accessing JS arrays based on indexes ".
Everything is an object:
Everything in JS is an object (a single dog is crying again), and arrays are a special form of objects. Using brackets to access array elements is the same as using square brackets to access object attributes. JS will convert the numeric index to the string format: 1-> "1 ". Therefore, The subscripts of the js array can be discontinuous, that is, the subscripts of a [2] can be directly followed by a [5] without a [3] and a [4].
Special Points:
The special feature of JS arrays is that when a non-negative integer is used as the attribute name, The JS array automatically maintains the length attribute value. The length attribute of Java is specified at the beginning and is not dynamically maintained.
An array is a special object that can be used to create attributes of any name. However, if the attribute used is an index of an array (that is, a number is used to access a digital index ), the special behavior of arrays is to update their length attributes as needed.
In early browsers, the efficiency of using JS arrays was not very high, because the data was accessed through array indexing. The JS engine's time consumption for searching JS array objects was similar to or even worse than accessing common attributes. However, many JS engines of modern browsers have optimized these functions, so the indexing method is quite fast. As for whether it is faster than querying common attributes, it has not been verified. JS arrays have two special actions when maintaining length values:
1. If the index of an element is greater than or equal to length, the length is changed to index + 1;
2. If length is set to be smaller than a certain value, all the elements whose index is greater than or equal to length are deleted;
Note:
In Java, the length attribute of the array is read-only, and the length attribute of the Java array is marked as final, so it cannot be modified during runtime, but JS allows us to read and modify it.
When accessing JS array elements, we should note whether we create an attribute for the object or the accessed array element. This affects the value of the JS length attribute.
For example:
A ["a"] is also allowed in the array, but it accesses the attribute a of array object.
A [1.00] is equivalent to a [1] is equivalent to a ["1"] is equivalent to a ["1.0"]. They all access the first element of array object, a committee's length attribute.
Traversal optimization
Because JS is an explanatory language, It queries local variables faster than object variables, and does not need to search for iteration through the prototype chain, in many cases, JS optimization uses a local variable to reference some variables with high query levels. Therefore, the JS array traversal optimization mainly involves two aspects: one is for the length attribute, and the other is for the array elements. caching them with two local variables can improve their efficiency.
Var temp = null; for (var I = 0, len = keys. length; I
Sparse array
JS arrays are also divided into sparse arrays and dense arrays, but JS sparse arrays are different from Java arrays. Arrays that are sparse enough in JS are generally slower to implement than dense arrays, but the memory usage is higher.
No sparse array is created using the direct array (that is, var a = [] method). The elements omitted in the JS array are undefined, which is slightly different from the element that does not exist. If this parameter is omitted in Java, the array elements are all null, that is, they occupy the memory space, but they are different in JS. In JS, you can use in to monitor the existence of array elements:
var a =[,,]0 in a;
JS array -- queue -- stack relationship
JS array is an object, so it can be considered as a Map. In fact, all JS objects can be considered as a Map. Therefore, we do not need to implement a HashMap too much in JS. In Java, we usually implement two data structures: Stack and queue through arrays. In JS, The JS array provides some column methods so that JS itself has the behavior of these two data structures.
(1) stack behavior: JS arrays have push and pop methods, which are the same as those defined in the data structure.
(2) queue behavior: The JS array also has the unsift and shift methods, which are used to insert and pop up data to the opponent. Combined with push and pop, you can start and join the queue.
(3) the most troublesome part of JS arrays is to delete intermediate elements. For example, to delete a [2], you can use the delete or splice method, but none of them are perfect.
Other methods:
(1) join connections constitute a string
(2) reverse elements
(3) sort sorting Array
Input a function to return a value. If the value is less than 0, the switching position is returned.
(4) concat connects two Arrays
(5) slice returns a segment or sub-array of the specified array.
The JS array also has many other "amazing" uses and optimization points. It seems that every language cannot be underestimated.