2. Data structure - array

Array definition

  1. Is a 相同类型data structure composed of elements.
  2. It holds an array of elements in memory 连续存储, and 索引accesses specific elements in the array through each element.
  3. array容量也就是长度会在一开始就被确定

advantage

  1. Fast Access: Since the elements of an array are stored in contiguous memory locations, any element of the array can be accessed quickly through subscripting. This makes array access very fast, especially when operating on elements, such as inserting, deleting, or updating elements.

  2. Efficient memory utilization: Because the elements in an array are stored in contiguous memory locations, arrays generally have higher memory utilization than other data structures such as linked lists.

  3. Easy to use: Array is a basic data structure that is easy to use and write. Most programming languages ​​provide native array types and subscript access operations, which make the use and operation of arrays very simple.

shortcoming

  1. Not easily extensible: In most programming languages, the size of arrays is fixed. If you need to add more elements, you usually need to rebuild a larger array, copying the elements from the original array into the new array. This may cause performance issues and additional memory consumption.

  2. Insertion and deletion operations are slow: When inserting or deleting elements into an array, all subsequent elements need to be moved, which may cause performance degradation, especially when the array size needs to be expanded.
    Insert image description here

  3. Not suitable for storing large data sets: Since arrays are stored contiguously in memory, it can cause memory issues when storing large data sets. Large arrays may exceed system memory limits and cannot be created.

array structure in js

After understanding the definition of array, we will find that arrays in js are extremely special. It can store a variety of data types and can expand its capacity at any time. It seems that it cannot be called an array at all.

Implementation Principle
In fact, arrays in js can be regarded as a special object implemented using a hash table.
A hash table is a data structure that stores data in the form of key-value pairs, where the keys are converted into corresponding indices through a hash function, and the values ​​are then stored at the corresponding positions in the array. Through the hash table method, the JavaScript array is actually a hash table inside, and each element is mapped to a different index position through a hash function.

Characteristics
Since arrays in JavaScript are implemented through objects, they have some characteristics of objects. For example, you can store any type of value in an array, such as numbers, strings, objects, functions, etc. Arrays can also contain other arrays or objects, forming multidimensional arrays or complex data structures.

Another important feature is that since an array is essentially an object, it inherits some of the methods and properties of the object. For example, you can use Object.keys() to get the key names in the array, Object.values() to get the values ​​in the array, and Object.hasOwnProperty() to determine whether a key exists in the array.

Disadvantages
However, it is important to note that arrays in JavaScript have some disadvantages compared to arrays in the traditional sense. JavaScript arrays can be slower when performing certain operations due to underlying implementation reasons. For example, when inserting or deleting elements, memory space needs to be reallocated and elements copied, which can result in slower performance. In addition, because the elements of the array are not stored contiguously, the performance of random access to elements may also be relatively low.

In general, js arrays can be regarded as objects covered with arrays, but because of the encapsulation support of the underlying API, they have some characteristics of arrays, and are even more powerful than the original array type.

Guess you like

Origin blog.csdn.net/qq_44473483/article/details/134814012