ArticlesArrays

Arrays

Contiguous arrays

Values stored in neighbouring memory, so an index is a jump and an insert in the middle has to move the tail.

A TypeScript array is a contiguous list. Neighbours in the array are neighbours in memory, which is why an index can be computed from the start address plus the offset. That jump is O(1). You do not walk from the front to reach element 10.

The same layout makes the middle expensive. Insert or delete anywhere but the end, and every later element has to shift along. That shift is O(n). Pushing at the end is cheap on average because nothing in front of the new value has to move.

This one fact explains a lot of later choices. Fast index access is an argument for staying with the array. Fast rewiring once you already hold a spot is an argument for a linked list. Pick the layout that matches the operation you actually repeat, not the one that is familiar.