The people in one row of cinema seats sit close together, with no empty seat between them. Someone who comes in late and sits at the end of the row bothers nobody. But if they want to sit in the middle, everyone to their right has to move one seat to make room.
An array works like that row of seats. An array keeps its items in boxes that sit side by side with no gaps, one item in each box. The boxes are the seats, and the items are the people. Each item has a position number (its index) starting at 0. In this lesson you'll see how much work each operation needs: reading, adding, inserting and removing.
Five operations on one array
The array arr starts as [10, 20, 30, 40, 50], then these five operations run in order:
| Step | Code | What moves | arr afterwards |
|---|---|---|---|
| 1 | arr[2] |
nothing (gives 30) |
[10, 20, 30, 40, 50] |
| 2 | arr.push(60) |
nothing | [10, 20, 30, 40, 50, 60] |
| 3 | arr.splice(3, 0, 35) |
40, 50, 60 move right | [10, 20, 30, 35, 40, 50, 60] |
| 4 | arr.splice(0, 1) |
6 items move left | [20, 30, 35, 40, 50, 60] |
| 5 | arr.pop() |
nothing | [20, 30, 35, 40, 50] |
Here are the same five operations as code:
const arr = [10, 20, 30, 40, 50];
console.log(arr[2]); // 30
arr.push(60); // [10, 20, 30, 40, 50, 60]
arr.splice(3, 0, 35); // [10, 20, 30, 35, 40, 50, 60]
arr.splice(0, 1); // [20, 30, 35, 40, 50, 60]
arr.pop(); // [20, 30, 35, 40, 50]
console.log(arr); // [20, 30, 35, 40, 50]
arr[2]reads the item at index 2. Indexes start at 0, so index 2 is the third item,30.arr.push(60)adds one item at the end.arr.pop()removes the last item, and the removed item is the result ofpop().arr.splice(3, 0, 35)inserts35at index 3. The first number is the position, the second (0) is how many items to remove, then comes the value to insert. To make room for35, the items40,50and60move one box to the right.arr.splice(0, 1)removes 1 item starting at index 0, which is10. The items after it move left to close the gap.splicechanges the array itself. If you still need the original array, use the operations that make a copy (in every major browser since 2023).[1, 2, 3].toSpliced(1, 1)works likesplice(1, 1), but gives a new array:[1, 3].[1, 2, 3].with(0, 9)gives a new array with the item at index 0 replaced by9:[9, 2, 3].
Fast at the end, slow in the middle
An array can't have gaps. So the amount of work depends on where the operation happens:
- Reading by index goes directly to the box. No other item is passed on the way.
- Adding or removing at the end (
push,pop) doesn't touch any other item. Sopushonto an array of a million items is just as fast aspushonto an array of five. - Inserting or removing in the middle or at the start forces every item after that position to move one box. This applies to
splice,unshift(adds at the start) andshift(removes the first item). - Searching for a value without knowing its index (
includes,indexOf) means checking the items one by one.
Programmers write this amount of work in Big O notation (a short way of writing it), which the next lesson covers. O(1) means the number of steps stays the same, however long the array is. O(n) means the number of steps grows with the number of items, written n: with twice the data, it takes roughly twice the time.
| Operation | Example | Big O |
|---|---|---|
| Read by index | arr[2] |
O(1) |
| Read the array's length | arr.length |
O(1) |
| Add or remove at the end | push, pop |
O(1) |
| Insert or remove in the middle or at the start | splice, unshift, shift |
O(n) |
| Search for a value | includes, indexOf |
O(n) |
So an array fits best for ordered data that you often read by index, and where you add or remove items at the end.
💡 Why can reading by index jump straight there? An array's items are stored right next to each other in memory. The computer knows the address of the first item (the number of its place in memory). So the address of the item at index
iis one calculation: the address of the first item plusitimes the size of one item. It stays one calculation, however long the array is. Now and thenpushneeds extra work: when the space set aside for the array is full, all its items are moved to a bigger space. But on average, eachpushis still O(1).
⚠️ What makes an array slow, or delete too much
unshiftandshiftin a big loop. Adding or removing at the start moves every other item. So a millionunshiftcalls are far slower than a millionpushcalls.splicewithout the second number.arr.splice(2)removes every item from index 2 to the end of the array, not just one. Writearr.splice(2, 1)when you want to remove only one item.- Calling
includesover and over. Every call checks the items one by one. If you often ask "is this value in there?", you'll meet Set later, a data structure that fits that question better. - Removing from the middle when order doesn't matter. Overwrite the item you want to remove with the last item. Now the last item is there twice, so call
pop()to remove the copy at the end. Nothing has to move.
TL;DR: An array stores items side by side with no gaps, with indexes (position numbers) starting at 0. Reading by index (arr[2]), reading length (the array's length), and push and pop at the end are all O(1): the number of steps stays the same, however long the array is. Inserting or removing in the middle or at the start (splice, unshift, shift) and searching for a value (includes) are O(n): the number of steps grows with the number of items. The reason is that the other items have to move, or are checked one by one. arr.splice(i, 1) removes one item at index i, and arr.splice(i, 0, x) inserts x at index i.
⚡ Read, and add at the end
Write:
const arr = [10, 20, 30, 40, 50];
console.log(arr[2]); // 30
arr.push(60); // [10, 20, 30, 40, 50, 60]
Guess first: why does arr[2] give 30 and not 20?
Indexes (the position numbers of an array's items) start at 0. So index 2 is the third item, 30.
Meaning: arr[2] reads the item at index 2. push adds at the end, and pop removes the last item. No other item is touched, so the number of steps stays the same, however long the array is. Programmers write this as O(1).
⚡ splice: insert and remove
Write:
arr.splice(3, 0, 35); // [10, 20, 30, 35, 40, 50, 60]
arr.splice(0, 1); // [20, 30, 35, 40, 50, 60]
Meaning: arr is the array from the step before. In splice, the first number is the position (an index), the second is how many items to remove, then comes the value to insert. splice(3, 0, 35) inserts 35 at index 3, and splice(0, 1) removes one item at index 0.
⚡ The middle is O(n)
Guess first: splice(3, 0, 35) runs on an array of a million items. Which items move?
Every item from index 3 to the end, each one index to the right. The longer the array, the more items move.
Meaning: Inserting or removing in the middle or at the start (splice, unshift, shift) moves other items, and searching for a value (includes) checks the items one by one. The number of steps grows with the number of items (n), so these are written O(n). Reading by index, length, push and pop stay O(1).
⚡ Watch out for splice without the second number
Meaning: arr.splice(2) removes every item from index 2 to the end of the array, not just one. Write arr.splice(2, 1) when you want to remove only one item.