An array stores elements in a single contiguous block of memory. Because
every element is the same size and sits next to the previous one, the address of
index i is just base + i * elementSize — a constant-time calculation.
Complexity
Operation
Time
Access by index
O(1)
Search (unsorted)
O(n)
Insert / delete at end
O(1) amortized
Insert / delete at front
O(n)
NOTE
“Amortized O(1)” for append means most pushes are cheap, but occasionally
the array doubles its capacity and copies everything — that rare O(n) cost
spreads out across all the cheap pushes.
A dynamic array
class DynamicArray<T> { private data: (T | undefined)[] = new Array(1); private size = 0; push(value: T): void { if (this.size === this.data.length) { // Grow by doubling — keeps append amortized O(1). const grown = new Array(this.data.length * 2); for (let i = 0; i < this.size; i++) grown[i] = this.data[i]; this.data = grown; } this.data[this.size++] = value; } get(index: number): T | undefined { return this.data[index]; }}
TIP
When you know the final size ahead of time, pre-allocate. You skip every
resize-and-copy cycle.
Arrays
An array is an ordered list of items stored right next to each other in memory — like numbered lockers in a row. Because they’re all the same size and side by side, the computer can jump straight to locker #7 instantly.
Why arrays are fast (and where they’re slow)
Since everything’s in a neat row:
Grabbing item by its number is instant — O(1). The computer does simple math to find its spot.
Adding/removing at the end is cheap — you’re not disturbing anything else.
Adding/removing at the front is slow — O(n). Everything after it has to shuffle over to make room (or close the gap).
Operation
Speed
Get item #i
O(1) — instant
Search for a value
O(n) — check each
Add/remove at end
O(1) — cheap
Add/remove at front
O(n) — everything shifts
Arrays that grow
Most languages (including JavaScript) give you arrays that grow automatically as you push. Under the hood, when they run out of room they quietly allocate a bigger chunk and copy everything over.
NOTE
That copy is occasionally slow, but it happens rarely (the array usually doubles its size), so on average each push is still cheap — this is called “amortized O(1).”
TIP
If you know how many items you’ll have, create the array at that size up front to skip the resizing.
In one sentence
An array stores items in a neat numbered row, so reading any item by index is instant and adding to the end is cheap — but adding/removing at the front is slow because everything has to shift.
Want to go deeper?
Switch to Expert mode above for the memory-address math and a hand-built dynamic array that doubles its capacity.