Abstract


Cache Hit

Elements of array are stored in Main Memory in a compact manner, thus making great use of Cache Locality

Fixed Size

If we want to expand, we have to create another bigger array & copy all the elements to the new array which is very time consuming

Element Removal

We can’t delete elements in arrays, we can only overwrite

Time Complexity

Contiguous Segment

  • A continuous range of Array

Dynamic Array


  • Also known as List
  • A Datatype that contains a Pointer to the underlying Array and other metadata like the capacity of the array and the current size of the array. As shown above, the purple blocks contain the metadata of the dynamic array. The yellow blocks are the actually array that hold the elements

Dynamic array mechanism visualisation

Convenient

Developers don’t need to re-write battle-tested logic of re-sizeing Array etc, battery-packed with best practices.

Secure

With the built-in expansion mechanism and length metadata, we are sure new elements aren’t added into Memory Address that belong to other parts of the Process (进程). Thus, ensuring Memory Safety.

More Resource Intense

We can’t fine tune every Array operations because the implementation details are abstracted away. We only have a limited interface to interact with it. And Dynamic array comes with metadata to support the different functionalities it offers.

Minimise re-sizing

If you know how big an array you want, it is usually recommended to set it as the capacity of your dynamic array. This reduce the need of frequent re-sizing operations which mean fewer allocation on Heap Segment. Thus, better performance.

Circular Array


  • Connect the start and end of the Array to form a loop
  • With taking remainder from frontIndex % arrayCapacity, we are able to implement circular array on an array. Take a look at Visual for better understanding
  • Used to implement Queue

Front Index

A variable to keep track the index for the start of the circular array.

Rear Index

A variable to keep track the index of the slot after the last element of the circular array, Can be obtained with frontIndex + arraySize.

JavaScript Array


Inserting a big index without the memory sacrifice

When inserting at a big index. We don’t need allocate an array in the Virtual Memory that large. It is just another key-value pair in the hash map!

References